Mvc_route Layer Depth

Source: Internet
Author: User

1. Introduction

Create a new MVC project and add home and about two controllers

Add the index page to the two controllers

namespacestudy_mvc_route.controllers{ Public classHomecontroller:controller {//Get:home         PublicActionResult Index () {returnView (); }         Public stringRegex (intYearintMonthintDay ) {            return$"{Year}-{month}-{day}";
$ is the inline parameter in the string }}}
namespace study_mvc_route.controllers{    publicclass  aboutcontroller:controller    {         // get:about         Public actionresult Index ()        {            return  View ();}}    }

Under the App_start folder, the RouteConfig.cs is the MVC routing configuration file

The main attributes are as follows:

  Name: route name  URL: a regular expression that matches a URL, with {} representing a parameter variable, which can be empty; no {} is a hard match requirement  defaults: By default, if the URL parameter is empty after matching the URL, the default action is used Controller   = default, action = Default action, id = parameter
Constraints:url parameter constraints
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Routing;namespacestudy_mvc_route{ Public classRouteconfig { Public Static voidRegisterRoutes (routecollection routes)//route set, matched by top down routing{routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");//Ignore route
//--------------------------------Add a route------------------------------------------ //URL to start with test
URL not introducedController parameter, so the controller uses the default value, and if you do not assign a value to the parameter, use the default value
routes. MapRoute (Name:"Test", URL:"Test/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional});





        //Route writing can omit named parameters, name, URL, defaults these named parameters can be omitted, as follows//URL for about can access Home/indexRoutes. MapRoute ("ABOUT1"," About",New{controller ="Home", action ="Index", id =urlparameter.optional}); //URL for about Access About/indexRoutes. MapRoute ("About2","{Controller}",New{controller ="Home", action ="Index", id =urlparameter.optional});
The larger brackets indicate the parameters, not the same as a normal string link, which is a hard match requirement







Comparison of the transfer parameters//using constraints to do parameter constraints//home/regex_2014_05_19routes. MapRoute (Name:"Regex", URL:"{Controller}/{action}_{year}_{month}_{day}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}, Constraints:New{year =@"\d{4}", month =@"\d{2}", day =@"\d{2}"}//@ "\d{4}" means input four digits, @ table cancel \ Translation Effect ); Common Communication Parameters//home/regex?year=2014&month=05&day=19routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); } }}

Mvc_route Layer Depth

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.