我创建了一个DelegatingHandler,它适用于每个请求。我已经阅读了一些文章,我可以为特定的路线定义一个处理程序,但我已经尝试了很多方法但没有成功,我错过了一些我没有找到的东西。
最后的尝试是:
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "SaveMessage",
routeTemplate: "api/message/{type}",
defaults: new { type = RouteParameter.Optional },
constraints: null,
handler: HttpClientFactory.CreatePipeline(
new HttpControllerDispatcher(GlobalConfiguration.Configuration),
new DelegatingHandler[]{new ValidationHandler()})
);
我的类控制器和方法具有以下属性:
[RoutePrefix("api/message")]
public class MessageController : ApiController
{
[Route("{type}")]
[HttpPost]
public HttpResponseMessage Save(string type, [FromBody] Message message)
{
....
}
}
我的处理程序缺少什么只适用于 api / message / text 等请求?
我已阅读How to make more MapHttpRoutes for MVC 4 Api和http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection等链接。
答案 0 :(得分:0)
问题是设置为null的约束。 您应该像这样定义它以使其适用于POST或GET:
constraints: new { httpMethod = new System.Web.Http.Routing.HttpMethodConstraint(HttpMethod.Post) }