Alex Rivera | Logout

Incorrect Url.Action when URL rewrite is used

Asked 2011-04-27T08:29:00.637
18

I get an incorrect URL from the Action method when I use a URL rewrite.

I have this rewrite rule in web.config:

<rule name="Old Objects 2" stopProcessing="true">
  <match url="^transportbilar/(nya|begagnade|miljobilar)/(.*)$" ignoreCase="true"/>
  <action type="Rewrite" url="/transportbilar/{R:2}"/>
</rule>

This will rewrite an URL like /transportbilar/nya/fiat/7s76s8dg to /transportbilar/fiat/7s76s8dg, which works fine, but the Url.Action gets confused by this.

I use an expression like this to create an URL in the page:

url.Action("Slideshow", "Object", new { id = objectId });

When the URL rewrite is not used (browsing directly to the page), this produces the correct URL /Object/Slideshow/7s76s8dg, but when the URL rewrite is used, the Action method adds the first part of the page URL to the generated URL, and produces the incorrect URL /transportbilar/Object/Slideshow/7s76s8dg.

I think that I can do a Redirect in the rewrite rule instead, which would circumvent the problem as the requesting URL would change, but is there a way to make it work without changing it to a Redirect?

Edit:

The routes that I think could possibly be relevant are these (added in this order):

transportbilar/handlare/{id}/{criteria}
transportbilar/handlare
transportbilar
transportbilar/sokresultat/{criteria}
transportbilar/{brand}/{id}/{criteria}
{controller}/{action}/{id}

The last route would catch the url /Object/Slideshow/7s76s8dg and the second from last would catch /transportbilar/fiat/7s76s8dg.

Edit
Report

1 Answer

3

Is the Url rewriting stuff for legacy Urls?

Could you perhaps ditch the Url rewrite and using a route with constraints. Something like:

routes.MapRoute(
    "OldObject2",
    "transportbilar/{mycondition}/{make}/{id}",
    new { controller = "Object", action = "Slideshow" },
    new
    {
        mycondition = "nya|begagnade|miljobila"
    }
);

I'm not sure exactly what your app is doing so the above might not make 100% sense but should give you an idea of what I'm thinking...

answered 2011-05-06T12:23:52.423

Your Answer