问题
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
How can I implement it?
I know this may be a duplicate (1, 2, 3), but I need help with it.
Thank you in advance
回答1:
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}
);
回答2:
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"}
);
}
}
回答3:
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"}
);
回答4:
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
:
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 }
);
来源:https://stackoverflow.com/questions/32112653/asp-net-webapi-routes-and-parameters-types