开发者

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.Writ开发者_如何学Goe("<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?


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;
 } 
 %>


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.


I found this one:

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


"./" 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\"  ...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜