我有WebAPI 2应用程序。如何指定2个或更多POST方法?
我有以下WebApiConfig:
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
和API控制器:
[RoutePrefix("api/books")]
public class BooksController : ApiController
{
[Route("Post1")]
[HttpPost]
public IQueryable<string> Post1(string str)
{
return null;
}
[Route("Post2")]
[HttpPost]
public IQueryable<string> Post2(int id)
{
return null;
}
}
我既不会打电话:
/api/books/post1
,也不
/api/books/post2
为什么以及如何解决它?
更新
问题解决了,问题在于简单类型作为参数。我收到404错误
Message =未找到与请求URI匹配的HTTP资源 'http://localhost:37406/api/books/post1'。
请求:
POST http://localhost:37406/api/books/post1 HTTP/1.1
User-Agent: Fiddler
Host: localhost:35979
Content-Type: application/json; charset=utf-8
{
"str" : "Fffff"
}
和代码:
[Route("Post1")]
[HttpPost]
public HttpResponseMessage Post1(string str)
{
return Request.CreateResponse();
}
[Route("Post2")]
[HttpPost]
public HttpResponseMessage Post2(int id)
{
return Request.CreateResponse();
}
但它适用于复杂类型:
[HttpPost]
[Route("Post1")]
public HttpResponseMessage Post1(Book book)
{
return Request.CreateResponse();
}
[HttpPost]
[Route("Post2")]
public HttpResponseMessage Post2(Book book)
{
return Request.CreateResponse();
}
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Genre { get; set; }
}
谢谢Nkosi
更新2:
但是当参数标有[FromBody]
时,它可以正常工作 [Route("Post1")]
[HttpPost]
public HttpResponseMessage Post1([FromBody]string str)
{
return Request.CreateResponse();
}
[Route("Post2")]
[HttpPost]
public HttpResponseMessage Post2([FromBody]int id)
{
return Request.CreateResponse();
}
(对于复杂类型,这是不必要的)。逻辑上,但路线错误混淆:)
答案 0 :(得分:1)
摘录自Attribute Routing in ASP.NET Web API 2
HTTP方法
Web API还会根据请求的HTTP方法选择操作 (GET,POST等)。默认情况下,Web API会查找不区分大小写的内容 与控制器方法名称的开头匹配。例如,a 名为PutCustomers的控制器方法匹配HTTP PUT请求。
您可以通过使用任何方法修饰mathod来覆盖此约定 以下属性:
[HttpDelete]
[HttpGet]
[HttpHead]
[HttpOptions]
[HttpPatch]
[HttpPost]
[HttpPut]
以下示例将CreateBook方法映射到HTTP POST请求。
[Route("api/books")]
[HttpPost]
public HttpResponseMessage CreateBook(Book book) { ... }
示例:
public class Book {
public int BookId{get;set;}
public string Title{get;set;}
public string Author{get;set;}
public string Genre{get;set;}
}
[RoutePrefix("api/books")]
public class BooksController : ApiController
{
// GET api/books
[Route("")]
public IEnumerable<Book> Get() { ... }
// GET api/books/5
[Route("{id:int}")]
public Book Get(int id) { ... }
// POST api/books
[HttpPost]
[Route("")]
public HttpResponseMessage Post1(Book book) { ... }
// POST api/books/alternate
[HttpPost]
[Route("alternate")]
public HttpResponseMessage Post2(Book book) { ... }
}
Post1
POST http://localhost:35979/api/books HTTP/1.1
User-Agent: Fiddler
Host: localhost:35979
Content-Type: application/json; charset=utf-8
Content-Length: 80
{
"Title":"Scary Book",
"Author":"John Doe",
"Genre":"Horror"
}
Post2
POST http://localhost:35979/api/books/alternate HTTP/1.1
User-Agent: Fiddler
Host: localhost:35979
Content-Type: application/json; charset=utf-8
Content-Length: 85
{
"Title":"Fantastic Book",
"Author":"Jane Doe",
"Genre":"Fantasy"
}