3

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?

Mantorok
  • 5,168
  • 2
  • 24
  • 32

3 Answers3

2

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 /.

Mantorok
  • 5,168
  • 2
  • 24
  • 32
  • this helped, but I also found that simply removing the tilde (~) worked for me. It shouldn't work that way, but it solved the problem. – Jacques Dec 09 '16 at 11:45
  • @Jacques Removing the tilde works as long as it's the root website. If you are running an application inside a website then you need that tilde or else it goes to the root website which is incorrect. – BVernon Nov 15 '17 at 06:35
1

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.

robrich
  • 13,017
  • 7
  • 36
  • 63
  • It doesn't just affect my content though, it also affects Html.BeginForm resulting in the incorrect route! – Mantorok May 27 '12 at 11:52
-1

Scripts.Url really helps to keep your app root in place

Albedo
  • 81
  • 1
  • 2
  • Welcome to SO! We appreciate your interest in helping people. Please use the 'answer' function if you can provide a solution to the asker's problem. For general advice, you may use the 'comment' function. – Twonky Jan 25 '20 at 17:47