を働いていないと: HttpMessageHandler MapHttpAttributeRoutesが、私はこのイメージにMessageHandler3の動作を取得しようとしている<a href="https://www.asp.net/web-api/overview/advanced/http-message-handlers" rel="nofollow">this tutorial</a>をfollwoingてる
これは私のコントローラのいずれかの一部です:
public class UserController : ApiController
{
[Route("api/users")]
[ResponseType(typeof(User))]
public IHttpActionResult PostUser([FromBody]User newUser)
{
try
{
userService.InsertUser(newUser);
return Ok(newUser);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
}
ですダミーバージョンのハンドラ:
public class UserTokenHandler : DelegatingHandler
{
public UserTokenHandler()
{
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
if (true)//actual validation method
{
var response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
var tsc = new TaskCompletionSource<HttpResponseMessage>();
tsc.SetResult(response);
return tsc.Task;
}
return base.SendAsync(request, cancellationToken);
}
}
ここでは、WebApiConfigの変更点を示しますクラス:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//config.MapHttpAttributeRoutes();
var handlers = new DelegatingHandler[] { new UserTokenHandler() };
var routeHandlers = HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(config), handlers);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: null,
handler: routeHandlers
);
config.MapHttpAttributeRoutes();
}
}
私の問題はコメント行にあります。私はそれを含めるか、最後にそれを置く(それが今であるように)ハンドラが実行されるが、私のAPIを使用しようとすると、私はこのエラーを取得しない場合:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:12986/api/users'.",
"MessageDetail": "No type was found that matches the controller named 'users'."
}
しかし、私はそれをアンコメント(および削除した場合一番下のもの)ハンドラを経由せずにコントローラに直接行きます。
ハンドラを実行してメソッドを見つけることができますが、どうすれば修正できますか?
最初の試合が優勝するため、マップの順序が重要です。コンベンショナルルートの前にアトリビュートルートをマップする – Nkosi