English 中文(简体)
IIS 7 rewrite rule and Url.Content issues
原标题:

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?

最佳回答

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

问题回答

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.

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





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签