(Before starting: I am aware of this and this. I'd like to find a more concise solution -if possible- for a slightly more specific problem)
I'm rewriting an old Webforms app in MVC. As usual, no permalinks should be broken.
I'm using standard {controller}/{action}/{id}
routes. Legacy paths are usually SomePage.aspx?ID=xxx
, and I have one particular case where Foo.aspx
is a list of Bar
(new URL: /Bar or /Bar/Index) and
Foo.aspx?ID=xxx
is the Bar
detail (new URL: /Bar/View/xxx)
One possible workaround is adding the following before the Default MapRoute
:
routes.MapRoute("Bar View", "Foo.aspx",
new { controller = "Bar", action = "View" });
And then defining the corresponding action in BarController:
public ActionResult View(int? id)
{
if (id == null)
return RedirectToAction("Index");
return View();
}
There are two problems with this:
- Now, if I create an ActionLink, it uses the legacy format
- I'd like to handle this in the routes; making the id nullable and redirecting in the controller is just wrong
I'm fine with mapping the legacy URLs by hand (I don't need a generic solution and there are only about 8 pages)
This is a new project, so I'm not tied to anything.