问题
I have the following rule in 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>
Which seems to work fine, however in my ASP.Net MVC 3 app I have several @Url.Content("~/") entries which are resolving to /store/ as the root as opposed to /. A typical url would be http://mysite.com/store/99/coats-with-hoods for example.
EDIT/UPDATE: I'm still pulling my hair out on this one, so I decided to look into Url.Content code-base and I noticed that it checks if the URL has been re-written (true) and if so it makes the path relevant, which in turn does not give me the absolute URL:
if (!PathHelpers._urlRewriterHelper.WasRequestRewritten(httpContext))
return contentPath;
string relativePath = PathHelpers.MakeRelative(httpContext.Request.Path, contentPath);
return PathHelpers.MakeAbsolute(httpContext.Request.RawUrl, relativePath);
Anyone know why this would be? I'm a bit confused as to why this is happening and how I can account for it in my application?
回答1:
Ok once I realised that I was never going to be able to use IIS Rewrite against ASP.Net MVC I decided to use HttpContext.RewritePath instead, and now all appears to be working as it should.
This is quite a fundamental issue as it wasn't just Url.Content that was affected, it was controller routes too, I had a form on this particular page that was also incorrectly pointing to /store/ instead of /.
回答2:
If your site is currently and will always be a the root of the domain/sub-domain (e.g. you always intend ~/
to mean site.com/
) then lose the ~
and just make all the urls be /path/to/content
. ~
does some wacky voodoo stuff -- as you've seen.
来源:https://stackoverflow.com/questions/10482299/iis-7-rewrite-rule-and-url-content-issues