asp.net-web-api - 为什么我的消息处理程序在 WebAPI 中未定义的情况下仍在运行?

标签 asp.net-web-api

我的配置中有以下设置:

routes.MapHttpRoute("NoAuthRequiredApi", "api/auth/", new { id = RouteParameter.Optional } );
routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }, null, new WebApiAuthenticationHandler());

如果我向 api/auth 上的 URL 发布任何内容,消息处理程序仍会运行并检查 Auth-Token header 。发生这种情况有什么原因吗?我应该更改 WebApi 路由的配置吗?在向身份验证 Controller 发出请求时,我显然不希望 header 上有任何身份验证 token ,因为此时我正在尝试检索 token 以在其他 Controller 上使用。

最佳答案

您最上面的路线永远不会匹配,因为没有指示需要哪个 Controller 。添加 Controller 名称作为默认值。 (如果不需要,请删除可选的 ID)。

所以:

routes.MapHttpRoute(
        name: "NoAuthRequiredApi",
        routeTemplate: "api/auth/",
        defaults: new { Controller = "Auth" }
    );

关于asp.net-web-api - 为什么我的消息处理程序在 WebAPI 中未定义的情况下仍在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13869541/

相关文章:

c# - 如何保护 ASP.NET Web API

c# - CreatedAtRoute路由到不同的 Controller

asp.net-web-api - 受 Azure AD B2C 保护的自动化端点测试

c# - WebApi2 请求的资源不支持post

c# - 自托管 Web API 的正确方法是什么?

asp.net-mvc-4 - MVC WebApi - System.MissingMethodException : Method not found: 'Void System.Net.Http.ObjectContent

c# - 如何将 Stream 转换为对象

asp.net-mvc-4 - 继承类型的 WebApi 模型绑定(bind)

c# - 在 .NET 后端移动服务的 InsertAsync 期间引发 HttpResponseException

c# - Web Api Request.CreateResponse HttpResponseMessage 没有智能感知 VS2012