2015-06-20 59 views
0

在我apicontroller我有2种方法可以处理POST请求:为什么我的Web Api 2 Post方法未命中?

public WatchListItemDTO Post(MovieDto movie) 
{ 
    //do smt.. 
} 

[HttpPost] 
[Route("MarkMovieAsWatched/{id}")] 
public void MarkMovieAsWatched(int id) 
{ 
    // do smt.. 
} 

该控制器具有前缀属性:[RoutePrefix("api/DownloadList")]。当我发出(发布)http://localhost:4229/api/DownloadList/MarkMovieAsWatched/的请求时,它会触发我的Post方法。该请求还包含一个对象:{id: 12}

WebApiConfig

public static void Register(HttpConfiguration config) 
{ 
    config.EnableCors(); 

    config.MapHttpAttributeRoutes(); 

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

    // To disable tracing in your application, please comment out or remove the following line of code 
    // For more information, refer to: http://www.asp.net/web-api 
    config.EnableSystemDiagnosticsTracing(); 
} 

可能有人explaint我为什么方法MarkMovieAsWatched不打?以及如何解决这个问题?

+0

这可能有所帮助:http://stackoverflow.com/a/9569381/1558122 –

回答

1

这可能是你的路线需要一个id作为路线的一部分。

试着改变你的属性:

[Route("MarkMovieAsWatched/{id?}")] 

,如果你没有通过ID作为路径的一部分通过这种方式,路线仍是一个有效的匹配。

相关问题