4

I have a application on asp.net mvc3 and working in a razor view. On my layout page I attach a css file and put some css in that file, but when I access /Account/LogOn view the css is not working.
It only works if I attach the css on Logon view. Anyone know why my css attached on the layout page is not working on /Account/LogOn?

I also tried by including following code:

@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "xyz.com – Login";
 }

It is still not working. Thanks in advance

3 Answers 3

5

Refer your css using url helper like this

<link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/default.css")"  media="screen" />
Sign up to request clarification or add additional context in comments.

2 Comments

Great , it works !!!!!! but what is the reason why it not work on normal html way ?
Because of root folder resolving. When you use html it cant find css file, because logOn view is in different folder. Html helper always find root.
3

In your _layout.cshtml, ensure that you use a path referencing the root (~) of your application. Use @Url.Content() to escape the path:

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

2 Comments

@smartboy Because MVC pages are dynamically created via a route, and the browser needs to go back to the server to fetch the css, it is impossible to use a static css route in _layout.cshtml which will work for different levels of path (/, /../, etc). Hence the need to reference css and js from the root, and map this with Url.Content
Erratum in above comment .. impossible to use a <del>static</del> variable css route ...
1

Create the stylesheet under the folder Contents by the name say abc.css

Now include the stylesheet in the LayoutPage so that it is included automatically in all the pages derived from the content page.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.