简体   繁体   English

为什么在尝试将数据发布到WebAPI操作时出现404错误?

[英]Why I got an 404 error when I try to post data to a WebAPI action?

Why I got an 404 error when I try to post data to a WebAPI action? 为什么在尝试将数据发布到WebAPI操作时出现404错误?

C#: C#:

public class ProductsController : ApiController
{
    [HttpPost]
    public List<Product> GetProductsByCategoryId(int categoryId, string title)
    {
        return new List<Product>
        {
            new Product { Id = 1 , Name = "Test" }
        };
    }
}

jQuery: jQuery的:

$.ajax({
    url: '/api/products',
    type: 'POST',
    data: {
        categoryId: 12,
        title: 'ABC'
    },
})

图片

I found the answer here . 我在这里找到了答案。

It's been quite sometime since I asked this question. 自从我问这个问题已经有一段时间了。 Now I understand it more clearly, I'm going to put a more complete answer to help others. 现在,我更加清楚地了解了这一点,我将提出一个更完整的答案来帮助他人。

In Web API, it's very simple to remember how parameter binding is happening. 在Web API中,记住参数绑定是如何发生的非常简单。

  • if you POST simple types, Web API tries to bind it from the URL 如果POST简单类型,网页API尝试从URL将它绑定
  • if you POST complex type, Web API tries to bind it from the body of the request (this uses a media-type formatter). 如果您发布POST复杂类型,则Web API会尝试从请求的正文中绑定它(这使用media-type格式化程序)。

  • If you want to bind a complex type from the URL, you'll use [FromUri] in your action parameter. 如果要从URL绑定复杂类型,请在action参数中使用[FromUri] The limitation of this is down to how long your data going to be and if it exceeds the url character limit. 这样做的限制取决于您的数据将要存储多长时间以及它是否超过了url字符限制。

    public IHttpActionResult Put([FromUri] ViewModel data) { ... }

  • If you want to bind a simple type from the request body, you'll use [FromBody] in your action parameter. 如果要绑定请求正文中的简单类型,请在操作参数中使用[FromBody]。

    public IHttpActionResult Put([FromBody] string name) { ... }

as a side note, say you are making a PUT request (just a string) to update something. 作为旁注,假设您正在发出PUT请求(只是一个字符串)以更新某些内容。 If you decide not to append it to the URL and pass as a complex type with just one property in the model, then the data parameter in jQuery ajax will look something like below. 如果您决定不将其附加到URL并作为仅在模型中具有一个属性的复杂类型进行传递,则jQuery ajax中的data参数将如下所示。 The object you pass to data parameter has only one property with empty property name. 您传递给data参数的对象只有一个属性为空的属性名。

 var myName = 'ABC'; $.ajax({url:.., data: {'': myName}}); 

and your web api action will look something like below. 您的网络api操作将如下所示。

 public IHttpActionResult Put([FromBody] string name){ ... } 

This asp.net page explains it all. 这个asp.net页面解释了所有这一切。 http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

tnx @Amila tnx @艾米拉

By default, Web API tries to match simple parameters from the URI, but you are supplying them in the body. 默认情况下,Web API尝试匹配 URI 中的简单参数,但是您在主体中提供它们。 In my original answer, I forgot you can't use FromBodyAttribute more than once. 在我的原始答案中,我忘记了您不能FromBodyAttribute使用FromBodyAttribute In this case, I'd create a model class to contain the values. 在这种情况下,我将创建一个包含值的模型类。 Web API will then read this "complex" type from the body. 然后,Web API将从主体读取此“复杂”类型。

public class ProductCategoryModel
{
   public int CategoryId { get; set; }
   public string Title { get; set; }
}

public class ProductsController : ApiController
{
    [HttpPost]
    public List<Product> GetProductsByCategoryId(ProductCategoryModel model)
    {
        return new List<Product>
        {
            new Product { Id = 1 , Name = "Test" }
        };
    }
}

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

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