MVC custom route 02-implement IRouteConstraint to limit the Controller name

Source: Internet
Author: User


By implementing the IRouteConstraint interface, you can restrict a control name. This article focuses on custom constraints. For more information, see:

MVC custom route 01-why do I need custom routing?

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

Effect

□Implement the IRouteConstraint Interface

using System;
using System.Web.Routing;
 
namespace MvcApplication2.Extension
{
    public class ExcludeController : IRouteConstraint
    {
        private readonly string _controller;
 
        public ExcludeController(string controller)
        {
            _controller = controller;
        }
        public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
// If the controller value obtained in the route is equal to the value set by the constraint, false is returned.
            return !string.Equals(values["controller"].ToString(), _controller, StringComparison.OrdinalIgnoreCase);
        }
    }
}
 

□Route adding Constraints

using System.Web.Mvc;
using System.Web.Routing;
using MvcApplication2.Extension;
 
namespace MvcApplication2
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
// Default
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                constraints: new { controller = new ExcludeController("RentalProperties") }
            );
        }
    }
}
 

Effect

As you can see, when you add custom constraints, the controller with the RentalProperties name will be restricted.

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.