(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.
I was able to solve this based on Dangerous' idea plus a constraint based on this answer.
My new route table is:
And the QueryStringConstraint couldn't be simpler:
I believe if you specify the following routes:
Then this should solve your first problem. If the user specifies Foo.aspx in the url then they will be taken to the View action.
If the action link:
is specified then the first route will be used (as the order matters).
However, I could not figure out how to specify if
Foo.aspx?id=...
then to go to one route else ifFoo.aspx
is specified then go to the other route. Therefore, I would check whether id is null in the action. However, if you do find this out I would very much like to know.Hope this helps.