Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Referencing stylesheets in the master page

I have a master page that is in /Views/Shared. The master page references a stylesheet in the /Content folder.

Everything works fine if I reference the stylesheet using "../../Content/style.css". However, my web application is not in the root folder in our production environment, so the relative path doesn't work.

I have tried "<%=ResolveUrl("~/content/style.css") %>" which does work in the production scenario, but then the designer in Visual Studio complains about my classes being wrong (and I cannot preview the page with CSS in the design tab).

Is there a solution that makes this work in both situations? I accomplished this in WebForms by writing server-side code that reset the link tag. I could do that here, but I would like to avoid it.

like image 671
Mike Therien Avatar asked Jan 20 '10 18:01

Mike Therien


People also ask

How to create layouts (master pages) in MVC 5?

Layouts (Master Pages) in ASP.NET MVC 5.0: Part Fourteen 1 . 2 In the application list panel, select ASP.NET Web application. 3 Give the name of your project, here I given it “ LayoutsMVCDemo ”. 4 Hit on to “Ok” button (Follow steps in sequence). ...

How do I reference properties and methods on the master page?

The rule for properties and methods is that you can reference them if they are declared as public members of the master page. This includes public properties and public methods. You can reference controls on the master page independently of referencing public members. Add a @ MasterType directive in the content page.

What is master page in ASP NET form?

Earlier in the ASP.NET Web Form there is a concept called master page, so that everything that is common for the website you can put into the master page, or in some cases you can create a Custom User Control for Header, Footer and Left Menu.

How do I specify the master page type of a content page?

Use the @MasterType directive to inform the ASP.NET engine of the content page's master page type. The @MasterType directive can accept either the type name of the master page or its file path. To specify that the AddProduct.aspx page uses Site.master as its master page, add the following directive to the top of AddProduct.aspx:


2 Answers

Try this technique - include your stylesheet both ways. Include one with a fixed path reference that Visual Studio will use for design-time support, but enclose it in server-side comments so it's not actually included during run-time. The second reference is the "real" reference used at run-time, and with Url.Content() it'll work whether your app is a sub directory or not.

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

<link href="<%=Url.Content("~/Content/Site.css") %>" rel="stylesheet" 
      type="text/css" />
like image 90
Kurt Schindler Avatar answered Oct 05 '22 23:10

Kurt Schindler


It is best practice to Extend the URL Helper. This allows you to easily call it from your view, and if your structure or files change, you don't need to do a massive find/replace.

public static string Image(this UrlHelper helper, string fileName)  
{  
    return helper.Content("~/Content/Images/" + fileName));  
}  

public static string Stylesheet(this UrlHelper helper, string fileName)  
{  
    return helper.Content("~/Content/Stylesheets/" + fileName);  
}  

public static string Script(this UrlHelper helper, string fileName)  
{  
    return helper.Content("~/Content/Scripts/" + fileName);  
}

   <link href="<%= UrlHelper.Stylesheet("Main.css")%>" rel="stylesheet" 
         type="text/css" />  
like image 42
Martin Avatar answered Oct 06 '22 01:10

Martin