Stefan Steiger
Stefan Steiger

Reputation: 82176

ASP.NET MVC and Response.Write in a master page

I have this code in my ASP.NET MVC project master page:

    <%
    switch(Request.Browser.Browser) 
    {
        case "IE": // Internet Explorer
            Response.Write("<link href=\"./Content/Site_IE.css\" rel=\"stylesheet\" type=\"text/css\" />");
            break;
        case "AppleMAC-Safari": // Chrome
            Response.Write("<link href=\"./Content/Site_FF.css\" rel=\"stylesheet\" type=\"text/css\" />");
            break;
        case "Firefox": // Firefox
            Response.Write("<link href=\"./Content/Site_FF.css\" rel=\"stylesheet\" type=\"text/css\" />");
            break;
        default: // All others
            Response.Write("<link href=\"./Content/Site_FF.css\" rel=\"stylesheet\" type=\"text/css\" />");
            break;
    }
%>

When I embed directly with:

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

then it works, but when I embed with the switch statement, then it doesn't find the location from views, but it finds it on the start page.

How can I map the path so it finds it from everywhere?

Upvotes: 0

Views: 1869

Answers (5)

Stefan Steiger
Stefan Steiger

Reputation: 82176

I found this one:

Response.Write("<link href=\""+Page.ResolveClientUrl("~/Content/Site_IE.css")+"\" rel=\"stylesheet\" type=\"text/css\" />");

Upvotes: 1

Mark
Mark

Reputation: 10206

I think I would rather see that decision made in the controller and the stylesheet passed in as a model property. You could test it easily and your markup would be a lot cleaner.

Upvotes: 1

mdm20
mdm20

Reputation: 4563

Try this:

     <%       
 switch(Request.Browser.Browser) 
 {
     case "IE": %> // Internet Explorer 
         <link href="<%= Url.Content ("~/Content/Site_IE.css") %>" rel="stylesheet" type="text"/css" />
         <% break;
     case "AppleMAC-Safari": %> // Chrome
         <link href="<%= Url.Content ("~/Content/Site_FF.css") %>" rel="stylesheet" type="text"/css" />                
         <% break;
     case "Firefox": %> // Firefox
         <link href="<%= Url.Content ("~/Content/Site_FF.css") %>" rel="stylesheet" type="text"/css" />                 
        <% break;
     default: %> // All others 
         <link href="<%= Url.Content ("~/Content/Site_FF.css") %>" rel="stylesheet" type="text"/css" />
        <% break;
 } 
 %>

Upvotes: 2

Mark Ewer
Mark Ewer

Reputation: 1835

Yeah, this was a pain at first. I wrote a blog post on how to get around this issue and I even put up some code for helper methods you can swipe from my site.

Upvotes: 0

womp
womp

Reputation: 116977

"./" means "from the current directory". Just use a path relative to the root of the application, starting with just a slash.

 Response.Write("<link href=\"/Content/Site_FF.css\"  ...

Upvotes: 0

Related Questions