asp.net – Web API小写路由

那里,

我需要在我的Web API项目中执行小写路由.
如果是一个MVC项目,我会使用类似的东西

routes.LowercaseUrls = true;

但在Web API中,该属性不存在.

我尝试了LowercaseRoutesMVC4 NuGet扩展,但我的路由需要一个自定义处理程序,以便扩展不能帮助我.

我能做什么?

解决方法

这看起来像你所需要的
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapHttpRoute(
            name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional },constraints: new { url = new LowercaseRouteConstraint() }
        );
    }
}

public class LowercaseRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext,Route route,string parameterName,RouteValueDictionary values,RouteDirection routeDirection)
    {
        var path = httpContext.Request.Url.AbsolutePath;
        return path.Equals(path.ToLowerInvariant(),StringComparison.InvariantCulture);
    }
}

我在https://gist.github.com/benfoster/3274578#file-gistfile1-cs-L4发现了这个

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....