的WebAPI路由 - 许多POST方法 [英] WebApi routing - many POST methods

查看:271
本文介绍了的WebAPI路由 - 许多POST方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WebAPI 2应用程序。我怎么可以指定2个或更多POST方法?

我有以下WebApiConfig:

 公共静态无效的注册(HttpConfiguration配置)
{
    CONFIG.SUP pressDefaultHostAuthentication();
    config.Filters.Add(新HostAuthenticationFilter(OAuthDefaults.AuthenticationType));    //网页API路线
    config.MapHttpAttributeRoutes();    config.Routes.MapHttpRoute(
        名称:DefaultApi
        routeTemplate:API / {}控制器/(编号),
        默认:新{ID = RouteParameter.Optional}
    );
}

和API控制器:

  [路线preFIX(API /书)]
公共类BooksController中:ApiController
{
    [路线(POST1)]
    [HttpPost]
    公众的IQueryable<串GT; POST1(字符串str)
    {
        返回null;
    }    [路线(POST2)]
    [HttpPost]
    公众的IQueryable<串GT; POST2(INT ID)
    {
        返回null;
    }
}

它的作品无论是我打电话:

  / API /书籍/ POST1

也没有

  / API /书籍/ POST2

为什么和如何解决呢?

更新:

问题就解决了​​,问题是在简单类型作为参数。我得到404错误


  

消息=无HTTP资源发现,请求URI相匹配
  的http://本地主机:37406 / API /书籍/ POST1


与要求:

  POST HTTP://本地主机:37406 / API /书籍/ POST1 HTTP / 1.1
用户代理:提琴手
主机:本地主机:35979
内容类型:应用程序/ JSON的;字符集= UTF-8{
    海峡:FFFFF
}

和code:

  [路线(POST1)]
    [HttpPost]
    公众的Htt presponseMessage POST1(字符串str)
    {
        返回Request.CreateResponse();
    }
    [路线(POST2)]
    [HttpPost]
    公众的Htt presponseMessage POST2(INT ID)
    {
        返回Request.CreateResponse();
    }

但它正常工作与复杂类型:

  [HttpPost]
   [路线(POST1)]
    公众的Htt presponseMessage POST1(书书)
    {
        返回Request.CreateResponse();
    }    [HttpPost]
    [路线(POST2)]
    公众的Htt presponseMessage POST2(书书)
    {
        返回Request.CreateResponse();
    }公共类图书
{
    公众诠释BOOKID {搞定;组; }
    公共字符串名称{搞定;组; }
    公共字符串作者{搞定;组; }
    公共字符串类型{搞定;组; }
}

感谢您恩科西

更新2:

但它工作时的参数标有[FromBody]

  [路线(POST1)]
    [HttpPost]
    公众的Htt presponseMessage POST1([FromBody]字符串str)
    {
        返回Request.CreateResponse();
    }
    [路线(POST2)]
    [HttpPost]
    公众的Htt presponseMessage POST2([FromBody] INT ID)
    {
        返回Request.CreateResponse();
    }

(对于复杂类型是不必要的)。在逻辑上,但路线错误困惑:)


解决方案

从<一所摘录href=\"http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#add-routes\"相对=nofollow>属性中路由的ASP.NET Web API 2


  

HTTP方法


  
  

网页API也选择基于请求的HTTP方法操作
  (GET,POST等)。默认情况下,网络API查找不区分大小写
  匹配与控制器方法名的开始。例如,一个
  名为PutCustomers控制器方法相匹配的HTTP PUT请求。


  
  

您可以通过装饰马托覆盖本公约的任何
  以下属性:


  [HttpDelete]
[HTTPGET]
[HttpHead]
[HttpOptions]
[HttpPatch]
[HttpPost]
[HttpPut]

下面的示例将CreateBook方法映射到HTTP POST请求。

  [路线(API /书)]
[HttpPost]
公众的Htt presponseMessage CreateBook(书书){...}

例如:

 公共类图书{
    公众诠释BOOKID {获取;设置;}
    公共字符串名称{获取;设置;}
    公共字符串作者{获取;设置;}
    公共字符串类型{获取;设置;}
}[途径preFIX(API /书)]
公共类BooksController中:ApiController
{
    //获取API /书籍
    [路线()]
    公共IEnumerable的&LT;图书&GT;获得(){...}    //获取API /书籍/ 5
    [路线({ID:INT})]
    公共图书获取(INT ID){...}    // POST API /书籍
    [HttpPost]
    [路线()]
    公众的Htt presponseMessage POST1(书书){...}    // POST API /书籍/备用
    [HttpPost]
    [路线(备用)]
    公众的Htt presponseMessage POST2(书书){...}
}

样POST身体 POST1

  POST HTTP://本地主机:35979 / API /书籍HTTP / 1.1
用户代理:提琴手
主机:本地主机:35979
内容类型:应用程序/ JSON的;字符集= UTF-8
内容长度:80{
    标题:可怕的书,
    作者:李四,
    类型:恐怖
}

样POST身体 POST2

  POST HTTP://本地主机:35979 / API /书籍/替代HTTP / 1.1
用户代理:提琴手
主机:本地主机:35979
内容类型:应用程序/ JSON的;字符集= UTF-8
内容长度:85{
    标题:奇书
    作者:李四,
    类型:幻想
}

I have WebAPI 2 application. How can I specify 2 or more POST methods?

I have the following 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 }
    );
}

and API Controller:

[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;
    }
}

It works neither I call:

/api/books/post1

nor

/api/books/post2

why and how to solve it?

UPDATE:

Problem is solved, problem was in simple types as parameters. I get 404 error

Message=No HTTP resource was found that matches the request URI 'http://localhost:37406/api/books/post1'.

with request:

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"
}

and code:

    [Route("Post1")]
    [HttpPost]
    public HttpResponseMessage Post1(string str)
    {
        return Request.CreateResponse();
    }


    [Route("Post2")]
    [HttpPost]
    public HttpResponseMessage Post2(int id)
    {
        return Request.CreateResponse();
    }

but it works fine with complex type:

   [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; }
}

Thank you Nkosi

UPDATE 2:

but it works when parameter is marked with [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();
    }

(for complex types it's unnecessary). Logically, but Route error confused :)

解决方案

Excerpt taken from Attribute Routing in ASP.NET Web API 2

HTTP Methods

Web API also selects actions based on the HTTP method of the request (GET, POST, etc). By default, Web API looks for a case-insensitive match with the start of the controller method name. For example, a controller method named PutCustomers matches an HTTP PUT request.

You can override this convention by decorating the mathod with any the following attributes:

[HttpDelete]
[HttpGet]
[HttpHead]
[HttpOptions]
[HttpPatch]
[HttpPost]
[HttpPut]

The following example maps the CreateBook method to HTTP POST requests.

[Route("api/books")]
[HttpPost]
public HttpResponseMessage CreateBook(Book book) { ... }

Example:

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) { ... }
}

Sample POST Body for 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" 
}

Sample POST Body for 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" 
}

这篇关于的WebAPI路由 - 许多POST方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆