我想动态地将路径设置为ApiController中的方法。下面显示了我的TokenController:
public class TokenController : ApiController
{
[Route("api/token/{grantType}")]
[RequireHttps]
public IHttpActionResult Post(string grantType)
{}
}
我正在考虑使用依赖注入,如下所示:
public class TokenController : ApiController
{
public TokenController(ITokenService tokenService)
{
//configure route "api/token/{grantType}" using tokenService?
}
[Route("api/token/{grantType}")]
[RequireHttps]
public IHttpActionResult Post(string grantType)
{}
}
或者我是否需要使用HttpConfiguration
对象在 App_Start 中执行此操作?
我该怎么做?
答案 0 :(得分:1)
找到我的答案。我将使用HttpConfiguration
:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "API TokenEndpoint",
routeTemplate: "services/newtoken/{grantType}",
defaults: new { controller = "Token" action="Post"},
constraints: null);
}
}