Asp.net web api Can';无法使web api属性路由正常工作

Asp.net web api Can';无法使web api属性路由正常工作,asp.net-web-api,asp.net-mvc-routing,Asp.net Web Api,Asp.net Mvc Routing,正在尝试调用此方法: [HttpGet] [Route("teacher/{teacherId}/student")] public IEnumerable<StudentDTO> GetByStudentByTeach(int id) { 我运行服务并在broswer中调用它: 在浏览器中,它返回404。我在fiddler中试过,它返回401。我猜401计划才是真正的问题,但这是否意味着这条路线正在运行,还有其他问题?我正在使用windows身份

正在尝试调用此方法:

    [HttpGet]
    [Route("teacher/{teacherId}/student")]
    public IEnumerable<StudentDTO> GetByStudentByTeach(int id)
    {
我运行服务并在broswer中调用它:

在浏览器中,它返回404。我在fiddler中试过,它返回401。我猜401计划才是真正的问题,但这是否意味着这条路线正在运行,还有其他问题?我正在使用windows身份验证


编辑:不是,我可以调用Get方法返回教师列表,使用通常的api/teacher。因此,我可以返回数据,但我无法使用此自定义方法。

您需要删除/api/然后重试

api/在maphttproute中定义,但在route中不存在属性


另外,更改方法的参数名称:GetByStudentByTeach(int-teacherId)

我将其删除,浏览器响应更改为:“未找到与请求URI“”匹配的HTTP资源”。此外,更改有效的参数GetByStudentByTeach(int-teacherId)的名称!所以诀窍是,route属性中的参数和method参数必须有匹配的名称。谢谢
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }