【问题标题】:ApiController get with id not workingApiController 获取 id 不起作用
【发布时间】:2018-05-08 16:43:50
【问题描述】:

我刚开始使用ApiController。我正在尝试发送一个 ID 的 HTTP GET,但它不起作用。

我的 ApiController:

[Route("api/Test")]
public class TestController : ApiController
{
    private myEntity db = new myEntity();

    [HttpGet]
    public HttpResponseMessage GetAll()
    {
        // Get a list of customers
        IEnumerable<Customer> customers = db.Customers.ToList();

        // Write the list of customers to the response body
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, customers);

        return response;
    }

    [HttpGet]
    public HttpResponseMessage GetById(int id)
    {
        // Get Customer by id
        Customer customer = db.Customers.Where(x => x.Id == id).FirstOrDefault();

        HttpResponseMessage response;

        if (customer == null)
        {
            response = Request.CreateResponse(HttpStatusCode.NotFound);
            return response;
        } else
        {
            response = Request.CreateResponse(HttpStatusCode.OK, customer);
        }

        return response;
    }

当我在浏览器中运行它时,GetAll 方法可以完美运行。然而,当我尝试GetById:

http://localhost:53198/api/Test/1

返回:

未找到与请求 URI http://localhost:53198/api/Test/1 匹配的 HTTP 资源

有谁知道我做错了什么?

【问题讨论】:

  • 您的网址不正确:http://localhost:53198/api/Test/GetById/1

标签: c# asp.net-web-api asp.net-web-api-routing


【解决方案1】:

如果使用属性路由,您需要进行一些更改以确保操作路由是不同的,以避免任何路由冲突。

[RoutePrefix("api/Test")]
public class TestController : ApiController {
    private myEntity db = new myEntity();

    //GET api/Test
    [HttpGet]
    [Route("")]
    public IHttpActionResult GetAll() {
        // Get a list of customers
        var customers = db.Customers.ToList();    
        // Write the list of customers to the response body
        return OK(customers);
    }

    //GET api/Test/1
    [HttpGet]
    [Route("{id:int}")]
    public IHttpActionResult GetById(int id) {
        // Get Customer by id
        Customer customer = db.Customers.Where(x => x.Id == id).FirstOrDefault();
        if (customer == null) {
            return NotFound();
        }
        return Ok(customer);
    }
}

这假定属性路由已启用

public static class WebApiConfig {
    public static void Register(HttpConfiguration config) {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

参考Attribute Routing in ASP.NET Web API 2

【讨论】:

  • RoutePrefix 有效。但是,如果我发送一个不在数据库中的 id,它只会在浏览器中给我一个空页面,而不是 NotFound 状态
  • 您必须修改函数以返回未找到。马上打电话。当我回到我的机器时会更新。
  • 谢谢...我在等。我知道“编辑函数”是什么
  • @MaxBoy 使用文档中建议的语法检查更新。
  • 谢谢...我将代码替换为使用 NotFound();但它没有改变任何东西。当我使用不存在的 ID 执行获取请求时,它只会返回一个空白页。我的意思是,它不会返回 NotFound 错误/警告
【解决方案2】:

你可以做任何一个

  • http://localhost:53198/api/Test/GetById/1(正如 DavidG 所说)

【讨论】:

  • 添加 [Route("{id:int}")] 对我不起作用。同样的错误
  • @MaxBoy 你能用 WebApiConfig 类的内容更新你的问题吗
  • 添加 RoutePrefix 就可以了。但是,如果我发送一个不在数据库中的 id,它只会在浏览器中给我一个空页面,而不是 NotFound 状态
  • @MaxBoy 如果您在数据库中找不到具有该 ID 的记录,则必须返回 404,它不会自动为您执行此操作
  • 嗯...我尝试使用 response = Request.CreateResponse(HttpStatusCode.NotFound);错了吗?
猜你喜欢
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2015-09-09
  • 2016-07-24
  • 2014-06-08
  • 1970-01-01
相关资源
最近更新 更多