Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I custom encode/decode route values?
    primarykey
    data
    text
    <p>I have this route:</p> <pre><code>{controller}/{id}/{action} </code></pre> <p>Because I think it makes more sense from RESTful perspective.</p> <p>The problem is that <code>id</code> can contain slashes (<code>/</code>) and those are treated as route separators even when encoded as "%2F". Even when I have this Web.config section in place:</p> <pre><code>&lt;uri&gt; &lt;schemeSettings&gt; &lt;add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes" /&gt; &lt;add name="https" genericUriParserOptions="DontUnescapePathDotsAndSlashes" /&gt; &lt;/schemeSettings&gt; &lt;/uri&gt; </code></pre> <p>Because I have <code>id</code> in the middle I can't employ <code>{*id}</code> approach which captures the rest of the route including the <code>action</code>.</p> <p>It looks like my only option is to encode <code>/</code> into an RFC compliant character like <code>!</code>, however I do not want to do it using ad-hoc custom code inside controller. I want controller to receive <code>id</code> intact, already decoded. And I want my <code>Url.Action</code> to generate properly encoded URL. Is that too much to ask from MVC, or do I need to scatter ActionFilters and custom URL helpers around?</p> <p>The only way I could find is to throw in a custom <code>IRouteConstraint</code> to manipulate the <code>RouteValueDictionary</code> it receives. That sounds like a dirty hack though: a constraint manipulating its input. God knows its side effects. Do you think this is a sane enough idea, or is there a better mechanism in ASP.NET MVC allowing that? </p> <p><strong>EDIT</strong>: This workaround only works when parsing the route, not when generating one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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