当前位置:首页 > IT技术 > Windows编程 > 正文

webapi 实现特效路由的自定义约束 大写约束
2021-10-20 10:35:47

约束的作用是 只有大写的参数值才能通过

 

一、定义约束

二、加载的时候引入约束

三、在controller 引入约束

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http.Routing;

namespace WebApi
{
    public class UpperCaseRouteConstraint : IHttpRouteConstraint
    {

        bool IHttpRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
        {
            // parameterName的意思是约束的参数名称 这里是  invalue
            // values 集合 包含参数名称,比如 invalue 的值。 
            object casevalue;
            if(values.TryGetValue(parameterName, out casevalue))// 这里获取 invalue 的值
            {
                return casevalue.ToString().ToUpper() == casevalue.ToString();
            }
            return false;

        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Routing;

namespace WebApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            DefaultInlineConstraintResolver constraintResolver = new DefaultInlineConstraintResolver();
            constraintResolver.ConstraintMap.Add("UpperCase", typeof(UpperCaseRouteConstraint));
            config.MapHttpAttributeRoutes(constraintResolver);

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Web.Http;
using WebApi.Properties;

namespace WebApi.Controllers
{
    public class ResourcesController : ApiController
    {
        [HttpGet]
        [Route("api/constrant/{invalue:UpperCase=TTT}")]
        public string GetValue(string invalue)
        {
            return invalue;
        }
    }
}

  

 

 获得定义一个新的 约束解析类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Routing;

namespace WebApi
{
    public class ExtendDefaultInlineConstraintResolver: DefaultInlineConstraintResolver
    {
        public ExtendDefaultInlineConstraintResolver()
        {
            this.ConstraintMap.Add("UpperCase", typeof(UpperCaseRouteConstraint));
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Routing;

namespace WebApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            //DefaultInlineConstraintResolver constraintResolver = new DefaultInlineConstraintResolver();
            //constraintResolver.ConstraintMap.Add("UpperCase", typeof(UpperCaseRouteConstraint));
            ExtendDefaultInlineConstraintResolver constraintResolver = new ExtendDefaultInlineConstraintResolver();
            config.MapHttpAttributeRoutes(constraintResolver);

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

  

 

本文摘自 :https://www.cnblogs.com/

开通会员,享受整站包年服务立即开通 >