Tuesday, 13 November 2012

MVC Routing Pages, Css


CSS Routing

If you want ignore routes for all css

RouteTable.Routes.IgnoreRoute("{resource}.css/{*pathInfo}");

Bare in mind that routing of css will not be accepted.

When you want to route css files you need to modify web.config

add into   system.web following

    <compilation>
    <buildProviders>
    <add extension=".css" type="System.Web.Compilation.PageBuildProvider"/>
    </buildProviders>
   </compilation>

and after you can add routing of CSS files

var themePath = CreateThemePath(themeName);
routes.MapPageRoute("layout.css", "layout.css", string.Format("{0}layout.css", themePath));


  private static string CreateThemePath(string themeName)
        {
            return string.Format("~/Content/themes/{0}/", themeName);
        }


After to display your css you need to help to the file with adding code inside of the file:
<%@ ContentType="text/css" %>

Note if you want to run old aspx pages within your mvc application you need to include following route. I am enclosing existing one that I am using.

routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\.(aspx|js|gif|png|jpg)(/.*)?" });
routes.RouteExistingFiles = true;