【问题标题】:Web API Attribute Routing URI IssueWeb API 属性路由 URI 问题
【发布时间】:2018-09-05 17:15:35
【问题描述】:

我想通过这个 URI 路由我的 API

localhost/api/ServiceA/1234

其中 isbn=1234

目前我正在从下面提到的 UrI 中检索 json

localhost/1234

其中 1234 是 isbn

如何使用以下 uri 获得相同的 json 结果?

localhost/api/ServiceA/1234

目前我在上面的 URL 中得到了 null

通过使用属性路由的以下代码,我得到了结果

我有一个 API 计数器

public class ServiceAController : ApiController
{
    [Route("api/ServiceA/{isbn}")]
    public Book GetBook(string isbn)
    {
        using (AppDbContext db = new AppDbContext())
        {
            var query = from b in db.Books
                        where b.ISBN == isbn && b.Source == "Book Store 1"
                        select b;
            return query.SingleOrDefault();
        }
    }
}

【问题讨论】:

    标签: c# asp.net asp.net-web-api2


    【解决方案1】:

    您似乎忘记为您的方法保留 [HttpGet] 属性。

    例如:

    [Route("api/ServiceA/{isbn}")]
    [HttpGet]
    public Book GetBook(string isbn){
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-24
      • 2014-11-27
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2021-08-12
      相关资源
      最近更新 更多