Asp.net IIS URL重写模块:URL.Content()未正确解析CSS/图像路径

Asp.net IIS URL重写模块:URL.Content()未正确解析CSS/图像路径,asp.net,asp.net-mvc,iis,relative-path,url-rewrite-module,Asp.net,Asp.net Mvc,Iis,Relative Path,Url Rewrite Module,我有一个标准的MVC3项目与布局页等,现在我需要作出漂亮的网址。我开始玩URL重写模块。我正在尝试翻译http://localhost/Photographer/Pablo进入http://localhost/category-about.aspx?displayName=Pablo,下面是我的重写规则(非常简单!): 而不是 http://localhost/Content/Site.css - which gives 200 当我请求此URL时:http://localhost/Photo

我有一个标准的MVC3项目与布局页等,现在我需要作出漂亮的网址。我开始玩URL重写模块。我正在尝试翻译
http://localhost/Photographer/Pablo
进入
http://localhost/category-about.aspx?displayName=Pablo
,下面是我的重写规则(非常简单!):

而不是

http://localhost/Content/Site.css - which gives 200
当我请求此URL时:
http://localhost/Photographer/Pablo
。逻辑工作正常-我的控制器收到请求并呈现页面-但缺少CSS和图像(因为它们的根文件夹前缀错误)。

您说过这行:

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" />

决议如下:

http://localhost/Photographer/Content/Site.css - and it gives 404
“http://localhost/photopher/Content/Site.css”


,这是绝对正确的。这就是解决问题的方法。css位于何处,css中图像的路径正确吗?

尝试使用Request.ApplicationPath。像这样的方法应该会奏效:

<link href="@(Request.ApplicationPath + "Content/Site.css")" rel="stylesheet" type="text/css" />

而不是这个

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

请在不使用波浪线(~)的情况下尝试此操作



这将解析为所需的
http://localhost/Content/Site.css

IIS 7.5如果有帮助的话……为什么你认为它是正确的?这应该是因为“~”会将您带到根虚拟目录,现在的问题是如何将其映射到IIS中。启动应用程序时,您得到的URL是什么?请参阅此链接:希望有帮助。到时候,我将尝试复制您面临的问题。当重写规则就位时,Url.Content中似乎有一个bug。我曾在其他几篇文章中提到过这一问题,但没有详细说明根本原因。实际上,我已经更新了我的响应,因为如果您依赖Request.ApplicationPath将应用程序根添加到路径中,则无需实际使用Url.Content。请注意,Request.ApplicationPath不包含尾部斜杠。我试图编辑答案来更新它,但它说我的更新必须是6个字符。我不想找另外5个角色来改变。
<link href="@(Request.ApplicationPath + "Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("/Content/Site.css")" rel="stylesheet" type="text/css" />