ASP. NET MVC Custom routing-implement Irouteconstraint limit controller name (reprint)

Source: Internet
Author: User

Before customizing constraints

namespacemvcapplication2{ Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); //defaultroutes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }    }}

Effect

After customizing constraints

Implementing the Irouteconstraint Interface

usingSystem;usingSystem.Web.Routing;namespacemvcapplication2.extension{ Public classExcludecontroller:irouteconstraint {Private ReadOnly string_controller;  PublicExcludecontroller (stringController) {_controller=Controller; }         Public BOOLMatch (System.Web.HttpContextBase HttpContext, Route route,stringParameterName, routevaluedictionary values,
Routedirection routedirection) {//returns False if the controller value obtained in the route is equal to the value set by the constraint return!string. Equals (values["Controller"]. ToString (), _controller, stringcomparison.ordinalignorecase); } }}

Route add constraint

usingSYSTEM.WEB.MVC;usingSystem.Web.Routing;usingmvcapplication2.extension;namespacemvcapplication2{ Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); //defaultroutes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}, Constraints:New{controller =NewExcludecontroller ("rentalproperties") }            ); }    }} 

Effect

Visible, with the custom constraint, the controller with the Rentalproperties name is limited.

ASP. NET MVC Custom routing-implement Irouteconstraint limit controller name (reprint)

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.