2017-01-10 3 views
0

Ich habe ein kleines Problem mit dem Routing in meiner App Angular + wep api. Ich möchte Benutzer, und es funktioniert nicht. Server gibt 404 und Fehler:Angular - Web Api Routing put Methode

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

und

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'."

Es ist seltsam, weil die Methode existiert.

Die Gesamtheit meines Codes folgt:

Meine Route:

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

Mein Put-Methode:

[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); 
    } 

Mein Winkel Put-Methode:

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; 
} 

Antwort

0

die angeben [FromUri] und [FromBody] zu def ine Parameter-Mapping

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

Sie müssen sicherstellen, dass die Post-Anforderung HTTP-Header enthält

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

Vielen Dank für Hilfe, aber ich Fehler nicht gefunden, ich gelöscht Parameter {userId} von Name Aktion und dann funktioniert es – ryckoshet