我在 IIS 7 中有以下规则:
<rule name="Category" enabled="true">
<match url="^store/([0-9]+)/(.*)" />
<action type="Rewrite" url="store?cid={R:1}" appendQueryString="false" logRewrittenUrl="true" />
</rule>
这似乎工作正常,但是在我的 ASP.Net MVC 3 应用程序中,我有几个 @Url.Content("~/") 条目解析为/store/作为根而不是/。一个典型的 url 是 http://mysite.com/store/99/coats-with-hoods例如。
编辑/更新:我还在纠结这个问题,所以我决定查看 Url.Content 代码库,我注意到它会检查 URL 是否已被重写 (true) 以及是否所以它使路径相关,这反过来又不给我绝对 URL:
if (!PathHelpers._urlRewriterHelper.WasRequestRewritten(httpContext))
return contentPath;
string relativePath = PathHelpers.MakeRelative(httpContext.Request.Path, contentPath);
return PathHelpers.MakeAbsolute(httpContext.Request.RawUrl, relativePath);
有人知道为什么会这样吗?我对为什么会发生这种情况以及如何在我的应用程序中解释它感到有点困惑?
最佳答案
好吧,一旦我意识到我永远无法对 ASP.Net MVC 使用 IIS 重写,我决定改用 HttpContext.RewritePath,现在一切似乎都在正常工作。
这是一个非常基本的问题,因为受影响的不仅仅是 Url.Content,它也是 Controller 路由,我在这个特定页面上有一个表单也错误地指向/store/而不是/。
关于asp.net-mvc - IIS 7 重写规则和 Url.Content 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10482299/