How to reference a .css file on a razor view?

For CSS that are reused among the entire site I define them in the <head> section of the _Layout: <head> <link href=”https://stackoverflow.com/questions/5021552/@Url.Content(“~/Styles/main.css”)” rel=”stylesheet” type=”text/css” /> @RenderSection(“Styles”, false) </head> and if I need some view specific styles I define the Styles section in each view: @section Styles { <link href=”https://stackoverflow.com/questions/5021552/@Url.Content(“~/Styles/view_specific_style.css”)” rel=”stylesheet” type=”text/css” /> } Edit: It’s … Read more

Razor View throwing “The name ‘model’ does not exist in the current context”

I think you have messed up the web.config file which lives in the Views folder. Create a new project targeting the same .NET framework and copy its Views/web.config file on top of the one in your current project. This will fix your problem. Also, as Dudeman3000 commented, if you have Areas in your MVC project … Read more

ASP.NET MVC 3 Razor: Include JavaScript file in the head tag

You can use Named Sections. _Layout.cshtml <head> <script type=”text/javascript” src=”https://stackoverflow.com/questions/4311783/@Url.Content(“/Scripts/jquery-1.6.2.min.js”)”></script> @RenderSection(“JavaScript”, required: false) </head> _SomeView.cshtml @section JavaScript { <script type=”text/javascript” src=”https://stackoverflow.com/questions/4311783/@Url.Content(“/Scripts/SomeScript.js”)”></script> <script type=”text/javascript” src=”https://stackoverflow.com/questions/4311783/@Url.Content(“/Scripts/AnotherScript.js”)”></script> }

What is the @Html.DisplayFor syntax for?

Html.DisplayFor() will render the DisplayTemplate that matches the property’s type. If it can’t find any, I suppose it invokes .ToString(). If you don’t know about display templates, they’re partial views that can be put in a DisplayTemplates folder inside the view folder associated to a controller. Example: If you create a view named String.cshtml inside … Read more

Preprocessor directives in Razor

I just created an extension method: public static bool IsDebug(this HtmlHelper htmlHelper) { #if DEBUG return true; #else return false; #endif } Then used it in my views like so: <section id=”sidebar”> @Html.Partial(“_Connect”) @if (!Html.IsDebug()) { @Html.Partial(“_Ads”) } <hr /> @RenderSection(“Sidebar”, required: false) </section> Since the helper is compiled with the DEBUG/RELEASE symbol, it works.

Serving favicon.ico in ASP.NET MVC

I agree with the answer from Chris, but seeing this is a specific ASP.NET MVC question it would be better to use either Razor syntax: <link rel=”icon” href=”https://stackoverflow.com/questions/487230/@Url.Content(“~/content/favicon.ico”)”/> Or traditionally <link rel=”icon” href=”https://stackoverflow.com/questions/487230/<%= Url.Content(“~/content/favicon.ico”) %>”/> rather than <link rel=”icon” href=”http://www.mydomain.com/content/favicon.ico”/>