WebApi控制器获取操作不起作用

时间:2014-04-18 20:50:23

标签: asp.net-web-api asp.net-web-api-routing asp.net-web-api2

我的一个WebApi2控制器出现问题。在编写测试时,我发现Get()从未被击中,而是返回301然后是403.奇怪的是,如果我点击Get(id),第二个动作会说出并完成它的工作,但是我永远不会打到Get ()行动。如果我重命名控制器它可以正常工作,但遗憾的是,我不能像我想的那样将ModelsController重命名为ModelController,因为有一个现有的用户群期望该名称。这是我认为在MVC2中完成的现有Api的重写。所有其他控制器都工作得很好,而不是这个。

有关如何调试此问题的任何想法?或者我可能错过了什么?

其他说明: 路由配置是默认的。 没有找到其他ModelsController

下面是我的代码的简化版本,问题仍然存在......

using System.Collections.Generic;
using System.Web.Http;
using TW.Api.Business.Services.Models;

namespace TW.Api.Business.Services.Controllers
{
    public class ModelsController : ApiController
    {
        public string Get()
        {
            return null;
        }

        public string Get(string id)
        {
            return null;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Routing;

namespace tw.api.business
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // config.MapHttpAttributeRoutes();


            config.Routes.MapHttpRoute(
                name: "Api_GetWithChild",
                routeTemplate: "{controller}/{id}/{action}/{childId}",
                defaults: new { id = RouteParameter.Optional, action = "Get", childId = RouteParameter.Optional },
                constraints: new { httpMethod = new HttpMethodConstraint("GET") });

            config.Routes.MapHttpRoute(
              name: "Api_Post",
              routeTemplate: "{controller}/{id}/{action}/{childId}",
              defaults: new { id = RouteParameter.Optional, action = "Post", childId = RouteParameter.Optional },
              constraints: new { httpMethod = new HttpMethodConstraint("POST") });

            config.Routes.MapHttpRoute(
            name: "Api_Put",
            routeTemplate: "{controller}/{id}/{action}/{childId}",
            defaults: new { id = RouteParameter.Optional, action = "Put", childId = RouteParameter.Optional },
            constraints: new { httpMethod = new HttpMethodConstraint("PUT") });

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

            // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
            // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
            // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
            //config.EnableQuerySupport();

            // To disable tracing in your application, please comment out or remove the following line of code
            // For more information, refer to: http://www.asp.net/web-api
            //config.EnableSystemDiagnosticsTracing();
        }
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace tw.api.business
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

2 个答案:

答案 0 :(得分:1)

查看您的路线配置,有一些问题

  1. 受限制的路线并非真正必要
  2. 路由模式中间的id是可选的并不真正起作用,你的可选项应该在最后。

答案 1 :(得分:1)

为web-API端点创建方法时,必须遵循一些命名约定,以自动映射method-to-api。或者您可以使用HTTP操作和路由属性

覆盖它

遵循以下任一安排(请注意&#39;模型&#39;模型&#39;方法名称):

public class ModelsController : ApiController
{
    public string GetModels()
    {
        return null;
    }

    public string GetModel(int id)
    {
        return null;
    }
}

或者,使用路线属性:

public class ModelsController : ApiController
{
    [HttpGet]
    [Route("api/Models")]
    public string GetModels()
    {
        return null;
    }

    [HttpGet]
    [Route("api/Models/{id}")]
    public string GetModel(string id)
    {
        return null;
    }
}

参考:Routing and Action Selection