ASP.NET Web Api 2 路由问题
ASP.NET Web Api 2 routing issue
我真的不明白为什么它不起作用。我有以下代码:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
[RoutePrefix("api/Profile")]
[System.Web.Http.AuthorizeAttribute]
[IdentityBasicAuthenticationAttribute]
public class ProfileApiController : ApiController
{
[HttpPost]
[ValidateApiContentLength]
[ValidateApiMimeMultipart]
[Route("Upload")]
public async Task<HttpResponseMessage> UploadDocumentAsync(string description)
{
//....
}
}
}
但是当我打电话时:http://localhost:11015/api/profile/Upload
我收到 404 错误:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:11015/api/profile/Upload'.",
"MessageDetail": "No action was found on the controller 'ProfileApi' that matches the request."
}
但洞察力指出错误:
哪里不对?
WebApi 路由找不到您的 UploadDocumentAsync 方法。在您的应用程序中,您同时使用了路由 table(在 WebApiConfig class 中)和属性路由。你不需要后者。
您可以在 WebApiConfig class 中保留路由 table 原样,并删除 Route 和 RoutePrefix属性。
将配置文件控制器中的 UploadDocumentAsync 操作更改为:
...
[HttpPost]
public async Task<HttpResponseMessage> UploadUploadDocumentAsync(string description)
{
...
只保留 HttpPost 属性。
您可以通过调用来访问您的资源(例如,您可以通过 Fiddler 实现):
POST http://localhost:11015/api/profile/
更新:
或者,如果您真的想在 url 中包含 "upload" 部分,您 可以 使用 Route 属性进行操作:
[Route("api/profile/upload")]
我找到了解决办法。问题不在路由中。问题出在动作参数上。它不应该用于 POST 方法。其他保持原样
[HttpPost]
[ValidateApiContentLength]
[ValidateApiMimeMultipart]
[Route("upload")]
public async Task<HttpResponseMessage> UploadDocumentAsync()
{
我真的不明白为什么它不起作用。我有以下代码:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
[RoutePrefix("api/Profile")]
[System.Web.Http.AuthorizeAttribute]
[IdentityBasicAuthenticationAttribute]
public class ProfileApiController : ApiController
{
[HttpPost]
[ValidateApiContentLength]
[ValidateApiMimeMultipart]
[Route("Upload")]
public async Task<HttpResponseMessage> UploadDocumentAsync(string description)
{
//....
}
}
}
但是当我打电话时:http://localhost:11015/api/profile/Upload
我收到 404 错误:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:11015/api/profile/Upload'.",
"MessageDetail": "No action was found on the controller 'ProfileApi' that matches the request."
}
但洞察力指出错误:
哪里不对?
WebApi 路由找不到您的 UploadDocumentAsync 方法。在您的应用程序中,您同时使用了路由 table(在 WebApiConfig class 中)和属性路由。你不需要后者。
您可以在 WebApiConfig class 中保留路由 table 原样,并删除 Route 和 RoutePrefix属性。
将配置文件控制器中的 UploadDocumentAsync 操作更改为:
...
[HttpPost]
public async Task<HttpResponseMessage> UploadUploadDocumentAsync(string description)
{
...
只保留 HttpPost 属性。
您可以通过调用来访问您的资源(例如,您可以通过 Fiddler 实现):
POST http://localhost:11015/api/profile/
更新:
或者,如果您真的想在 url 中包含 "upload" 部分,您 可以 使用 Route 属性进行操作:
[Route("api/profile/upload")]
我找到了解决办法。问题不在路由中。问题出在动作参数上。它不应该用于 POST 方法。其他保持原样
[HttpPost]
[ValidateApiContentLength]
[ValidateApiMimeMultipart]
[Route("upload")]
public async Task<HttpResponseMessage> UploadDocumentAsync()
{