2017-01-10 10 views
0

私のアプリでのルーティングに少し問題があります。角度+ wep api。私はユーザーを入れたい、それは動作しません。サーバーは404とエラーを返します:角度 - Web APIのルーティング方法

Failed to load resource: the server responded with a status of 404 (Not Found)

Message":"No HTTP resource was found that matches the request URI ' http://localhost:53544/api/Users/PutUser/1 '.","MessageDetail":"No action was found on the controller 'Users' that matches the name 'PutUser'."

方法が存在するので、それは奇妙です。

私のコードの全体は、次のとおりです。

マイルート:

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

私のputメソッド:

[HttpPut] 
    [ActionName("PutUser/{userId}")] 
    [ResponseType(typeof(void))] 
    public IHttpActionResult PutUser(int userId, User user) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     if (userId != user.Id) 
     { 
      return BadRequest(); 
     } 

     db.Entry(user).State = EntityState.Modified; 

     try 
     { 
      db.SaveChanges(); 
     } 
     catch (DbUpdateConcurrencyException) 
     { 
      if (!UserExists(userId)) 
      { 
       return NotFound(); 
      } 
      else 
      { 
       throw; 
      } 
     } 

     return StatusCode(HttpStatusCode.NoContent); 
    } 

マイ角度putメソッド:

this.PutUser = function (userId, user) { 
    var promise = $http({ 
     method: 'PUT', 
     url: 'api/Users/PutUser/' + userId, 
     data: user 
    }) 
    .then(function (response) { 
     return "update"; 
    }, 
    function (response) { 
     return response.statusText; 
    }); 
    return promise; 
} 

答えて

0

を指定[FromUri]と[FromBody]からdefあなたはPOSTリクエストのHTTPヘッダーが助けを

Content-Type: application/x-www-form-urlencoded 
+0

おかげが含まれていますが、私はエラーを発見したことを確認する必要があり伊根パラメータマッピング

[HttpPut] [ActionName("PutUser/{userId}")] [ResponseType(typeof(void))] public IHttpActionResult PutUser([FromUri]int userId, [FromBody]User user) { 

は、私はアクション名からパラメータ{userIdを}を削除し、それが動作します – ryckoshet

関連する問題