Asp.net MVC 5 Routing, mvcrouting

Source: Internet
Author: User

Asp.net MVC 5 Routing, mvcrouting

ASP. net mvc is a classic model-view-controller mode applicable to WEB applications. Compared with web forms, asp.net mvc is composed of various code layers connected together.

Recently, I have been involved in the asp.net mvc project, and I have forgotten a lot of my memories.

First, it is about Routing.

ASP. net mvc no longer depends on physical pages. You can use your own syntax to customize URLs and use these syntax to specify resources and operations. The syntax is expressed by a set in URL mode, also known as a route.

A route is a pattern matching string representing the absolute URL path. Therefore, a route can be a constant string or contain some placeholders.

Create an asp.net mvc project. In the global. asax file, we can see that the route is registered here, so that the program can be processed at startup.

 

The common routing features include name, URL mode, and default value.

Let's see what the route for the new project is like. We open RouteConfig

namespace HEAMvcDemo{    public class RouteConfig    {        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }            );        }    }}

MapRoute is usually used to fill in the static set of mvc route object management. The MapRoute method provides many heavy loads.

The first parameter is the route name. Here it is set to Default, and each route has a unique name.

The second parameter is the URL mode.

The third parameter is the default value of the URL-specified parameter.

 

We can customize conditions to constrain routing.

For example, the id must be a five-digit number.

routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },                          new {id=@"\d{5}"}            );

So what is the meaning of routes. IgnoreRoute ("{resource}. axd/{* pathInfo ??

This Code instructs the routing system to ignore any. axd request.

 

When we click run on the newly created page, we can see that the address of the browser is http: // localhost: 49627/. Which controller view is opened in one operation ??

In route configuration, new {controller = "Home", action = "Index", id = UrlParameter. optional}, which defines the controller view opened by default. When the url is not in the complete URL mode, → {controller}/{action}/{id }.

Let's take a look at this URL http: // localhost: 49627/Home/About

Controller = Home action = About id = ?? So what about id? Why is there no id here? It is because id = UrlParameter. Optional, where id is an Optional parameter

 

This article is my learning record. If you have any questions, please comment below. repost the article to indicate the source.

If you have any help, please give me a thumbs up at the bottom right of the mouse. Your support is my greatest motivation.

Related Article

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.