简体   繁体   中英

Web API Attribute Routing URI Issue

I want to route my API through this URI

localhost/api/ServiceA/1234

where isbn=1234

currently I am retrieving json from the UrI mentioned below

localhost/1234

where 1234 is isbn

How can I get the same json results with the following uri?

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

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.

Ex:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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