MVC 3 keeping short url

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 06:24:00

问题


I have MVC3 application, where I want to keep short URL. what is the best or clean way to do this? Lets say I have two controllers Account and Home. I have all the account related tasks Logon, Logoff, Profile, FAQs etc. in Account controller. All the main tasks in home controller like TaskA, TaskB, and TaskC. I am looking for URL as below:

  1. www.mydomain.com/Logon
  2. www.mydomain.com/Logoff
  3. www.mydomain.com/Profile
  4. www.mydomain.com/FAQs
  5. www.mydomain.com/TaskA
  6. www.mydomain.com/TaskB

when user first come to the website they need to redirect to Logon page. At any time user should also able to switch from once controller action to another controller action (from TaskA to Logoff).

what is the clean way to do this?


回答1:


You don't need to set a route for each URL. With a little help from route constraints you can do something like this:

        routes.MapRoute(
            "Home", // Route name
            "{action}", // URL with parameters
            new { controller = "Home", action = "Index" }, // Parameter defaults
            new { action = "TaskA|TaskB|TaskC|etc" } //Route constraints
        );

        routes.MapRoute(
            "Account", // Route name
            "{action}", // URL with parameters
            new { controller = "Account", action = "Logon" }, // Parameter defaults
            new { action = "Logon|Logoff|Profile|FAQs|etc" } //Route constraints
        );



回答2:


You can set a route for the specific urls that do not match the default route. For example:

routes.MapRoute("Logon", "logon/", new { controller = "account", action = "logon" });
routes.MapRoute("TaskA", "TaskA/", new { controller = "home", action = "taska" });

Your default route can define your start page if all other matches for the url are not found.

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}/", // URL with parameters
            new { controller = "account", action = "logon", id = UrlParameter.Optional } // Parameter defaults


来源:https://stackoverflow.com/questions/6406301/mvc-3-keeping-short-url

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