Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to change the querystring variable in ASP.NET MVC path before it hits the controller?
    primarykey
    data
    text
    <p>I have a controller method in ASP.NET MVC that looks like this:</p> <pre><code>public ActionResult GetAlbumPictures(int albumId) { var album = AlbumRepo.GetSingle(albumId); var pictures = album.Pictures; return View(pictures); } </code></pre> <p>The routing for this method looks like this:</p> <pre><code>routes.MapRoute(null, "pictures" new { controller = "Album", action = "GetAlbumPictures" }); </code></pre> <p>The user will use the following URL to get the pictures, filtered by the album ID:</p> <pre><code>GET http://server/pictures?albumid=10 </code></pre> <p>However, I'd like to change the querystring parameter to just <code>album</code> instead of <code>albumid</code>:</p> <pre><code>GET http://server/pictures?album=10 </code></pre> <p>This would mean that the controller method needs to be modified to:</p> <pre><code>public ActionResult GetPictures(int album) { ... } </code></pre> <p>However, this is not ideal because now the method has a parameter named <code>album</code>, which can be confused as an <code>Album</code> <strong>object</strong> instead of the <code>ID</code> of the <code>Album</code>.</p> <p>My question is, is there any way of configuring ASP.NET MVC so that in the routing, it will receive a querystring parameter called <code>album</code>, but then pass it off to the controller as the <code>albumId</code> parameter?</p> <p><strong>P.S.</strong> I know that I can do this in the routing table:</p> <pre><code>routes.MapRoute(null, "album/{albumId}/pictures", new { controller = "Album", action = "GetAlbumPictures" }); </code></pre> <p>But due to legacy issues, I have to make it work for the querystring method as well.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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