ASP. NET MVC Routing Jin Jie (bis)--Custom routing constraints

Source: Internet
Author: User
Tags httpcontext

3. Custom Routing constraints

What is a custom routing constraint? If the routing format is Archive/{year}/{month}/{day}, where Year,month,day is constrained, it must be a number and a certain range.

At this point, we can set the constraint class and make custom routing constraints.

First step: We first add the year limit class Yearrouteconstraint. Please look at the code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Routing;usingSystem.Globalization;namespacemvcmobiledms.app_start{ Public classYearrouteconstraint:irouteconstraint { Public BOOLMatch (HttpContextBase HttpContext, Route route,stringparametername, routevaluedictionary values, routedirection routedirection) {            if(routedirection = = Routedirection.incomingrequest && parametername.tolower (cultureinfo.invariantculture) = =" Year")            {                Try                {                    intYear = Convert.ToInt32 (values[" Year"]); if(Year >=1900) && (Year <=2100))return true; }                Catch                {                    return false; }            }            return false; }    }}

Step two: Add the month limit class Monthrouteconstraint. Please look at the code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Routing;usingSystem.Globalization;namespacemvcmobiledms.app_start{ Public classMonthrouteconstraint:irouteconstraint { Public BOOLMatch (HttpContextBase HttpContext, Route route,stringparametername, routevaluedictionary values, routedirection routedirection) {            if(routedirection = = Routedirection.incomingrequest && parametername.tolower (cultureinfo.invariantculture) = ="Month")            {                Try                {                    intmonth = Convert.ToInt32 (values["Month"]); if(Month >=1) && (Month <= A))return true; }                Catch                {                    return false; }            }            return false; }    }}

Step Three: Add the date restriction class Dayrouteconstraint. Please look at the code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Routing;usingSystem.Globalization;namespacemvcmobiledms.app_start{ Public classDayrouteconstraint:irouteconstraint { Public BOOLMatch (HttpContextBase HttpContext, Route route,stringparametername, routevaluedictionary values, routedirection routedirection) {            if(routedirection = = Routedirection.incomingrequest && parametername.tolower (cultureinfo.invariantculture) = =" Day")            {                Try                {                    intmonth = Convert.ToInt32 (values["Month"]); intDay = Convert.ToInt32 (values[" Day"]); if(Day <1)return false; Switch(month) { Case 1:                         Case 3:                         Case 5:                         Case 7:                         Case 8:                         Case Ten:                         Case  A:                            if(Day <= to)return true;  Break;  Case 2:                            if(Day <= -)return true;  Break;  Case 4:                         Case 6:                         Case 9:                         Case  One:                            if(Day <= -)return true;  Break; }                }                Catch                {                    return false; }            }            return false; }    }}

OK, the three restricted classes are finished, and you need to configure the routes in the global application class, as shown in the code listing:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Routing;namespacemvcmobiledms{ Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="DMS", action ="logon", id =urlparameter.optional}); Routes. MapRoute ("Archive",                "Archive/{year}/{month}/{day}",                New{controller ="Archive", action ="Index", year ="", month ="", day =""},New{year =NewMvcMobileDMS.App_Start.YearRouteConstraint (), month =NewMvcMobileDMS.App_Start.MonthRouteConstraint (), day =NewMvcMobileDMS.App_Start.DayRouteConstraint ()}); }    }}

Let's look at the results of the operation:

When I enter the following address in the browser address bar, HTTP://LOCALHOST:7449/ARCHIVE/1988/9/10, as follows:

And when I enter an address that does not meet the constraints, HTTP://LOCALHOST:7449/ARCHIVE/1988/09/34 is as follows:

Thus, when an illegal route is entered, the routing system is treated as an error, so we can set constraints on the MVC route to standardize the routing format.

Well, it's written here today. Hope to be helpful to you O (∩_∩) o haha ~

ASP. NET MVC Routing Jin Jie (bis)--Custom routing constraints

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.