ApiController 路由覆盖不起作用

ApiController route override not working

我有问题,在我的 Asp.Net OWIN 自托管 Web 服务路由中用 Route 属性覆盖不起作用。

我的初始路由是这样的:

string routeTempl = "api/" + EndpointManager.API_VERSION + "/{controller}/{action}/{id}";

// Configure Web API for self-host. 
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: routeTempl,
defaults: new { id = RouteParameter.Optional }
);

我的控制器是这样的:

public class GeneralController : ApiController
{
    /// <summary>
    /// Ping the api, mostly for testing reason
    /// </summary>
    /// <returns>Current server datetime</returns>
    [HttpGet]
    public DateTime Ping()
    {
        // return datetime as ping
        return DateTime.Now;
    }

    /// <summary>
    /// Get the current api version
    /// </summary>
    /// <returns>Api-Version as a string</returns>
    [HttpGet, Route("~/api/version")]
    public string ApiVersion()
    {
        return EndpointManager.API_VERSION;
    }
}

ping 方法可以访问,如我所愿,结束:http://localhost:48000/api/v1-0/general/ping

但我无法通过以下方式访问 ApiVersion-Method:http://localhost:48000/api/version

有人知道我在 Route-属性中做错了什么吗?

谢谢!

尝试在路由配置后添加该行:

config.MapHttpAttributeRoutes();