简体   繁体   English

Web API属性路由URI问题

[英]Web API Attribute Routing URI Issue

I want to route my API through this URI 我想通过此URI路由我的API

localhost/api/ServiceA/1234

where isbn=1234 其中isbn = 1234

currently I am retrieving json from the UrI mentioned below 目前我正在从下面提到的UrI中检索json

localhost/1234

where 1234 is isbn 其中1234 isbn

How can I get the same json results with the following uri? 如何在以下uri中获得相同的json结果?

localhost/api/ServiceA/1234

Currently I am getting null with the above URL 目前,我无法使用上述网址

With the following code using attribute routing I am getting the results with 通过以下使用属性路由的代码,我得到了结果

I have an API Countroller 我有一个API Countroller

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();
        }
    }
}

Looks like you forgot to keep the [HttpGet] attribute for your method. 您好像忘记了为方法保留[HttpGet]属性。

Ex: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM