ASP.NET的WebAPI路线和参数类型 [英] ASP.NET WebApi Routes and parameters types
本文介绍了ASP.NET的WebAPI路线和参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看了很多文章,类似的问题,但没有这些决定,不适合我。
我有3种方法:
I have read many articles and similar questions but none of those decisions do not fit me. I have 3 methods:
public string GetA()
{
return "Hello from GetA";
}
public string GetB(int id)
{
return "Hello from GetB";
}
public string GetC(sting all)
{
return "Hello from GetC";
}
我需要配置类似的路线:
I need to configure route like:
1.http://localhost:63087/api/Test/
2.http://localhost:63087/api/Test/all
3.http://localhost:63087/api/Test/1
4.http://localhost:63087/api/Test/1/all
我怎样才能实现呢?结果
我知道这可能是一个重复的( 1 ,的 2 ,的 3 ),但我需要它的帮助。
How can I implement it?
I know this may be a duplicate (1, 2, 3), but I need help with it.
感谢您提前
推荐答案
其他的解决方法是静态部分添加到路线:
Other solution is to add static segment to route:
1.http://localhost:63087/api/Test/
2.http://localhost:63087/api/all/Test/
3.http://localhost:63087/api/Test/1
4.http://localhost:63087/api/all/Test/1
实施
public string GetA()
{
return "Hello from GetA";
}
public string GetB(int id)
{
return "Hello from GetB";
}
[Route("api/all/{controller}/{id}")]
[Route("api/all/{controller}")]
public string GetC(int id= 0)
{
return "Hello from GetC";
}
路由配置:
config.Routes.MapHttpRoute(
name: "AllRoute",
routeTemplate: "api/full/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional}
);
这篇关于ASP.NET的WebAPI路线和参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文