Note that there are some explanatory texts on larger screens.

plurals
  1. POIncorrect Url.Action when URL rewrite is used
    text
    copied!<p>I get an incorrect URL from the <code>Action</code> method when I use a URL rewrite.</p> <p>I have this rewrite rule in web.config:</p> <pre><code>&lt;rule name="Old Objects 2" stopProcessing="true"&gt; &lt;match url="^transportbilar/(nya|begagnade|miljobilar)/(.*)$" ignoreCase="true"/&gt; &lt;action type="Rewrite" url="/transportbilar/{R:2}"/&gt; &lt;/rule&gt; </code></pre> <p>This will rewrite an URL like <code>/transportbilar/nya/fiat/7s76s8dg</code> to <code>/transportbilar/fiat/7s76s8dg</code>, which works fine, but the <code>Url.Action</code> gets confused by this.</p> <p>I use an expression like this to create an URL in the page:</p> <pre><code>url.Action("Slideshow", "Object", new { id = objectId }); </code></pre> <p>When the URL rewrite is not used (browsing directly to the page), this produces the correct URL <code>/Object/Slideshow/7s76s8dg</code>, but when the URL rewrite is used, the <code>Action</code> method adds the first part of the page URL to the generated URL, and produces the incorrect URL <code>/transportbilar/Object/Slideshow/7s76s8dg</code>.</p> <p>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?</p> <h3>Edit:</h3> <p>The routes that I think could possibly be relevant are these (added in this order):</p> <pre><code>transportbilar/handlare/{id}/{criteria} transportbilar/handlare transportbilar transportbilar/sokresultat/{criteria} transportbilar/{brand}/{id}/{criteria} {controller}/{action}/{id} </code></pre> <p>The last route would catch the url <code>/Object/Slideshow/7s76s8dg</code> and the second from last would catch <code>/transportbilar/fiat/7s76s8dg</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload