无法在 ASP.NET MVC 母版页中生成自定义路径
我正在 ASP.NET MasterPage 中工作,并且在 方面遇到问题。
我试图用特定名称替换样式表:
<link href="/Content/Styles/<%=Model.Style%>.css" rel="stylesheet" type="text/css" />
不幸的是,这会创建 HTML 输出:
<link href="/Content/Styles/<%=Model.Style%>.css" rel="stylesheet" type="text/css" />
这显然不是预期的结果。
如果我将相同的代码放入视图占位符中,它就可以完美运行。这不是一个好的解决方案,因为我有很多页面,我只想让它做同样的事情。
看起来它正在尝试自动更正 URL - 有没有办法将其关闭?
编辑1:
我已经使用以下方法暂时修复了这个问题:
<link href=<%=String.Format("\"/Content/Styles/{0}.css\"", Model.Style)%> rel="stylesheet" type="text/css" />
I am working in an ASP.NET MasterPage and am having trouble with <link href="..." />
.
I am trying to substitute in a stylesheet with a specific name:
<link href="/Content/Styles/<%=Model.Style%>.css" rel="stylesheet" type="text/css" />
Unfortunately, this creates the HTML output:
<link href="/Content/Styles/<%=Model.Style%>.css" rel="stylesheet" type="text/css" />
Which is clearly not what was intended.
If I put the same code in a View placeholder, it works perfectly. This is not a good solution though as I have many pages where I just want it to do the same thing.
It looks like it's trying to automatically correct the URL - is there a way to switch this off?
Edit 1:
I have fixed this temporarily using:
<link href=<%=String.Format("\"/Content/Styles/{0}.css\"", Model.Style)%> rel="stylesheet" type="text/css" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的站点部署在虚拟文件夹中,您的问题和迄今为止发布的解决方案中的所有链接都将失败。相反,请执行以下操作:
这 (1) 解决了您问题中的问题,并且 (2) 允许您的站点在虚拟文件夹中工作。
All the links in your question and in the solution posted thus far will fail if your site is deployed in a virtual folder. Instead, do:
This (1) fixes the problem in your question, and (2) allows your site to work in a virtual folder.
试试这个:
丑陋但有效。
Try this:
Ugly but works.