search

Home  >  Q&A  >  body text

asp.net-mvc - How to do routing in asp.net mvc?

Asp.net mvc is usually

controller/action/parameters

Address form of

But most of the time, the website address is not such a path

like:

xxxx.com/asp.net/
xxxx.com/asp.net/mvc/
xxxx.com/asp.net/webform/
xxxx.com/asp.net/ado.net/

xxxx.com/javascript/
xxxx.com/javascript/jquery/
xxxx.com/javascript/angularjs/
xxxx.com/javascript/jichu/

xxxx.com/jiaoyu/

At this time, the address is not in the form of controller/action/parameter

I don’t know how to map this kind of path in asp.net mvc? ? ?

为情所困为情所困2783 days ago604

reply all(1)I'll reply

  • 阿神

    阿神2017-05-16 17:08:19

    This depends on how your routing rules are defined and the order in which the routes are defined.
    If this request enters the ASP.NET pipeline model, it will go to the predefined route to perform matching. When it encounters the first matching route, it will directly return the routing result.
    For example, the first routing rule of the default route is:

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

    xxxx.com/asp.net/ will match this routing rule, controller="asp.net", action="Login";
    xxxx.com/asp.net/mvc/ will also match this routing rule, controller="asp.net", action="mvc";

    Suppose you define another routing rule, and this routing rule is in front of the routing rule just now:

        routes.MapRoute(
            name: "asp.net",
            url: "asp.net/{controller}/{action}/{id}",
            defaults: new { controller = "User", action = "Link", id = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Admin", action = "Login", id = UrlParameter.Optional }
        );
    

    xxxx.com/asp.net/ will match this routing rule, controller="User", action="Link";
    However, xxxx.com/cast/ will not match the first routing rule, but will continue to match with the second routing rule. At this time, the match will be successful, controller="cast", action="Login"

    It is recommended that you read the book "Mastering the ASP.NET MVC3 Framework". Chapter 11 Routing Rules provides an analysis and introduction to this issue.

    reply
    0
  • Cancelreply