How could I route to the /Home/Index action regardless of the URL?

╄→尐↘猪︶ㄣ 提交于 2019-12-31 06:58:07

问题


I am looking for a way to route all traffic to my Home controller and to the Index action, regardless of the URL they use to arrive at my site.

As long as they are using www.MyDomain.com, a user visiting www.MyDomain.com/Controller1/Action1 would need to all be routed to the same action as a user visiting www.MyDomain.com/Controller2/Action3/ID6

I can redirect each one of these manually but some of the URL's may be dynamically generated and therefore I need to learn how to catch all and redirect.

Many thanks for reading.


回答1:


When setting your routes, you may try defining a default route as the last route definition

    routes.MapRoute(
        "Default",
        "{*any}",
        new { controller = "Home", action = "Index" }
    );

Hope this will help




回答2:


There exists a file Global.asax.cs in root of your solution where logic for routing is written, modify the logic according to your requirement.



来源:https://stackoverflow.com/questions/14585519/how-could-i-route-to-the-home-index-action-regardless-of-the-url

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