2017-01-05 11 views
0

WebAPIアクションにデータを投稿しようとすると404エラーが表示されるのはなぜですか?WebAPIアクションにデータを投稿しようとすると404エラーが表示されるのはなぜですか?

の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:デフォルトでは

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

image

+0

url: '/ api/products'?製品とは何ですか? – onur

+0

あなたはメソッドを呼び出していません。パスが '/ api/products'の場合は、'/api/products/GetProductsByCategoryId'メソッド名を呼び出す必要があります。 – kosmos

+0

@Satpal、正しいイメージを追加します。私のルートの設定が 'config.Routes.MapHttpRouteある –

答えて

1

答えは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.

  • if you POST simple types, Web API tries to bind it from the URL
  • if you POST complex type, Web API tries to bind it from the body of the request (this uses a media-type formatter).

  • If you want to bind a complex type from the URL, you'll use [FromUri] in your action parameter. The limitation of this is down to how long your data going to be and if it exceeds the url character limit.

    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.

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

as a side note, say you are making a PUT request (just a string) to update something. 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. The object you pass to data parameter has only one property with empty property name.

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

and your web api action will look something like below.

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

This asp.net page explains it all. http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

tnx @Amila

0

、ウェブAPIは、URIからmatchの簡単なパラメータにしようとしていますが、体内でそれらを供給しています。私の元の答えでは、FromBodyAttributeを複数回使用することはできません。この場合、値を格納するモデルクラスを作成します。 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" } 
     }; 
    } 
} 
+0

を呼び出すと、' [FromBody] '属性を2つ入れることはできません。エラー: '複数のパラメータ( 'categoryId'と 'title')をリクエストのコンテンツにバインドできません。 ' –

+0

これは正しいです。私の削除された答えの私の間違いは、これはパラメータではなくモデルでなければならないことを明確にしていませんでした。 – G0dsquad

関連する問題