2017-05-03 6 views
0

を見つけることができませんなぜ私は$httpサービス

getUserPost: function(id) { 
    var posts = []; 
    var req = { 
     method: "POST", 
     url: "/api/getPost", 
     data: id 
     }; 

によってコントローラに要求を送信しようと、アプリケーションを持っている私はいつも次の取得エラー:

Possibly unhandled rejection: {"data":{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:57196/api/getPost'.","MessageDetail":"No action was found on the controller 'Post' that matches the request."},"status":404,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/getPost","data":0,"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"Not Found"} 

コントローラー:

[RoutePrefix("api")] 
public class PostController : ApiController 
{ 
    private readonly DataContext _db = new DataContext(); 

    [Route("getPost")] 
    [HttpPost] 
    public List<Post> GetPost(string id) 
    { 
     var posts = new List<Post>(); 
     if (id != null) 
     { 
      posts = _db.Posts.Where(x => x.UzytkownikId == int.Parse(id)).ToList(); 

     } 
     return posts; 
    } 

誰がどのように私は目を解決することができます知っていますありますか?

+1

は 'データを試してみてください。JSON.stringify({ID:ID})' –

答えて

1

アクションルートを間違った方法で定義しました。このように、あなたの行動にFromBodyを追加してみてください:

[Route("getPost")] 
    [HttpPost] 
    public List<Post> GetPost([FromBody]string id) 
    { 
     var posts = new List<Post>(); 
     if (id != null) 
     { 
      posts = _db.Posts.Where(x => x.UzytkownikId == int.Parse(id)).ToList(); 

     } 
     return posts; 
    } 
+0

OMG、ではないが、多くの時間前には、この属性なしで動作しますが、なぜsomethisましたかわった ?あなたの答えをありがとう!助けになる。 –

関連する問題