How to route legacy type urls in ASP.NET MVC

余生长醉 提交于 2019-12-07 03:07:36

The parameter val within your BackwardCompatibleAction method should be automatically populated with the query string value. Routes are not meant to deal with query strings. The solution you listed in your question looks right to me. Have you tried it to see what happens?

This would also work for your route. Since you are specifying both the controller and the action, you don't need the curly brace parameter.

routes.MapRoute(
    "dosomething.asp Backward compatibility",
    "dosomething.asp",
    new { controller = "MyController", action = "BackwardCompatibleAction"}
);

If you need to parametrize the action name, then something like this should work:

routes.MapRoute(
    "dosomething.asp Backward compatibility",
    "{action}.asp",
    new { controller = "MyController" }
);

That would give you a more generic route that could match multiple different .asp page urls into Action methods.

http://www.bob.com/dosomething.asp?val=42 would route to MyController.dosomething(int val)

and http://www.bob.com/dosomethingelse.asp?val=42 would route to MyController.dosomethingelse(int val)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!