ASP.Net MVC 4 layout changing


Im trying to convert an html template to ASP.Net MVC 4 project. Bbut i have run in an problem. Then opening the localHost:11062/ site everything looks prefectSe here. But if i try same address just calling the controller and action direct localHost:11062/StartMenu/Index, which should be the same, but it's notSe here. For me it looks like the css file isent loaded correct. But if it wasent the start site should not look different?

Problem courtesy of: mortenstarck


I suspect that you have hardcoded the url to some of your CSS files, just like this:

<link href="Content/Site.css" rel="stylesheet" type="text/css" />

instead of using an url helper:

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

Of course the same is true for all static resources such as javascript, images, ...

You should never hardcode urls in an ASP.NET MVC application. Always use url helpers when dealing with urls.

Also if you are referencing some static resources (such as images) in your CSS file, don't forget that they should be relative to the location of the CSS file.

You might easily see this in the Net tab of a javascript debugging tool such as FireBug where you would get 404 errors for the corresponding resource.

Solution courtesy of: Darin Dimitrov


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.