Ben*_*nji 1 routing asp.net-web-api asp.net-web-api-routing
使用web api时,如果使用,如何调用正确的路由方法 [RoutePrefix()]
假设您有类似"MyReallyLongNamedClassController"的内容.默认路由为http:... com/api/MyReallyLongNamedClass.然后应用程序通过名为Get,Post,Put等的方法(除非当然使用动词装饰器).
如果我[RoutePrefix("api/LongClass")]在我的控制器上放置一个路由前缀装饰器,我怎么能让web api仍然使用这些方法的默认值?
意思是,我希望名为"GetAll()"的方法仍然映射到"api/LongClass"(当使用get头时)和"PostThis(int id)"仍然映射到"api/LongClass/{id}"(当使用帖子标题时)
这是我为解决问题而做的,而不必用注释来装饰所有方法.我把RoutePrefix放在类级别,以及默认的Route
[RoutePrefix("api/longclass")]
[Route("{id?}")]
public class MyReallyLongNamedClass: ApiController
{
public string GetAll(int id)
{
return "result";
}
public string PostThis([FromBody] MyModel model)
{
var res= _repository.Save(model);
return res;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10529 次 |
| 最近记录: |