Routing for a default document in MVC5, eliminating action from URL

梦想与她 提交于 2020-04-30 12:21:04

问题


I currently have a login area that is supposed to be the first page a logged-out user will visit. It is located at the Index action of a LoginController. Under default circumstances, this page would be accessed by visiting the URL:

http://<url>.<tld>/Login/Index

I'd like to make this page accessible from the root URL - http://<url>.<tld>. To achieve that, I assumed that the following rout configuration would suffice:

routes.MapRoute(
                name: "Default",
                url: "",
                defaults: new { controller = "LoginController", action = "Index", id = UrlParameter.Optional }
            );

However, this merely results in 404. I would be perfectly content with appending login to the URL (http://<url>.<tld>/Login), but modifying the route configuration like so:

url: "login"

Also results in 404. Additionally, entering the root URL alone now results in 403 (forbidden).

After some more digging, I came across some conflicting information. Some sources state that custom routing in MVC5 should actually be placed in the Global.asax.cs file, whilst other sources refer specifically to RouteConfig.cs. Which is correct? Does it matter?

Furthermore, adding an additional MapRoute method above the Default method, whilst leaving the Default method in the template state, makes no difference.

How do I modify the route to manage completely customised URL entries, whilst setting a "default document" or it's MVC equivalent? For the latter, I am guessing that simply adjusting the direction of the Default MapRoute method will suffice - assuming I can get the syntax correct.

来源:https://stackoverflow.com/questions/57414340/routing-for-a-default-document-in-mvc5-eliminating-action-from-url

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