[英]ASP.NET MVC and Response.Write in a master page
I have this code in my ASP.NET MVC project master page: 我的ASP.NET MVC项目母版页中有以下代码:
<%
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. 然后它可以工作,但是当我嵌入switch语句时,它不会从视图中找到位置,而是在开始页面上找到它。
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.
您可以轻松地对其进行测试,并且标记会干净很多。
我发现了这个:
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.
我写了一篇有关如何解决此问题的博客文章,甚至为您可以从我的网站上滑动的辅助方法提供了一些代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.