"Go" ASP. NET MVC definition short URLs

Source: Internet
Author: User

In the MVC logic Code, the Controller and action are required, but in the URL, there is no need to fully embody the Controller and action. For example, we often want to see http://localhost/About rather than http://localhost/Home/About.

Default routing Rules

After you create a new MVC application, the routing rules that are registered by default in Global.asax are:

?
public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");             routes.MapRoute(                "Default", // Route name                "{controller}/{action}/{id}", // URL with parameters                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults            );        }

We can define our own routing rules.

Define short URLs

We define a routing rule that can be equivalent to http://localhost/Home/About with Http://localhost/About:

?
routes.MapRoute(    "ShortAbout",    "About",    new{ controller = "Home", action="About"});

Access Http://localhost/About and Http://localhost/Home/About are the same at this time.

The above statement defines only a short URL, for the sake of universality, you can define a routing rule as follows:

?
routes.MapRoute(    "ActionOnly",     "{action}/{id}",     new{ controller = "Home", action = "About", id = UrlParameter.Optional });

To reduce the effect on the default action (URL no action) under other controllers, you can limit the action:

?
routes. MapRoute (      " Actiononly "      "{Action}/{id}"      new {controller = "Home" "about"       new {action = " about| Index " } );


"Go" ASP. NET MVC definition short URLs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.