[英]ASP.NET WebApi Routes and parameters types
I have read many articles and similar questions but none of those decisions do not fit me. 我已经阅读了许多文章和类似的问题,但是这些决定都不适合我。 I have 3 methods:
我有3种方法:
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
How can I implement it? 我该如何实施?
I know this may be a duplicate ( 1 , 2 , 3 ), but I need help with it. 我知道这可能是一个重复( 1 , 2 , 3 ),但我需要它的帮助。
Thank you in advance 先感谢您
Try this code: 试试这个代码:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/Test",
defaults: new { controller = "Test", action = "GetA"}
);
config.Routes.MapHttpRoute(
name: "WithID",
routeTemplate: "api/Test/{id}",
defaults: new { controller = "Test", action = "GetB", id = UrlParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ALL",
routeTemplate: "api/Test/all",
defaults: new { controller = "Test", action = "GetC"}
);
}
}
Configure your routes like this 像这样配置您的路线
http://localhost:63087/api/Test
http://localhost:63087/api/Test/1
http://localhost:63087/api/Test/1/all
Something like this: 像这样:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/Test",
defaults: new { controller = "Test", action = "GetA"}
);
config.Routes.MapHttpRoute(
name: "Route2",
routeTemplate: "api/Test/{id}",
defaults: new { controller = "Test", action = "GetB" }
);
config.Routes.MapHttpRoute(
name: "Route3",
routeTemplate: "api/Test/{id)/{all}",
defaults: new { controller = "Test", action = "GetC"}
);
So, you can not have two routes with same parameter count. 因此,不能有两个具有相同参数计数的路由。
2.http://localhost:63087/api/Test/all
3.http://localhost:63087/api/Test/1
In this way, you can use in all your methods parameters type string
: 这样,您可以在所有方法中使用
string
类型的参数:
public string GetA()
{
return "Hello from GetA";
}
public string GetB(string id, string all = "")
{
if (id.Equals("all") || all.Equals("all"))
{
return "Hello all from GetB";
}
return string.Format("Hello {0} from GetB", id);
}
Route config: 路由配置:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "RouteWithParam",
routeTemplate: "api/{controller}/{id}/{all}",
defaults: new { all = RouteParameter.Optional }
);
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
Implementation: 实现方式:
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";
}
Route config: 路由配置:
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}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.