Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to duplicate all routes as a pretend subdirectory
    primarykey
    data
    text
    <p>To create a mobile version of a site I would like to reuse my controllers, some views etc. but create what looks like a subdirectory for a mobile version. So you could have URLs like:</p> <pre><code> /Controller/Action/Id /mobile/Controller/Action/Id etc. </code></pre> <p>These routes would ideally map to the same controller and then I could look somewhere like in the route data to see if the URL is a mobile one. It would be good I can use <code>@Html.ActionLink()</code> etc. within a view and by default stay on either the mobile or desktop version without having to specify this, but have the ability to force one or the other.</p> <p>I can achieve something like this by having an optional parameter on the front of every route, e.g. </p> <pre><code>routes.MapRoute( "Default", "{mobile}/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional, mobile = UrlParameter.Optional }, new { mobile = "mobile|" } ); </code></pre> <p>This then works as expected:</p> <pre><code>Takes you to either / or /mobile version depending on where you are currently: @Html.ActionLink("Current", "Index"); / version @Html.ActionLink("Desktop", "Index", new { mobile = "" }); /mobile version @Html.ActionLink("Mobile", "Index", new { mobile = "mobile" }); </code></pre> <p>However this seems like a really lame solution, because I don't really want to add that to the front of every route. Is there any nice way I can do this in a better way, for example adding the optional <code>/mobile</code> prefix to all routes and have this work correctly? Or alternatively is there a completely different way I could approach the whole problem which would make more sense?</p> <p>Hope this isn't too vague, any advice would be very much appreciated.</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.
    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