asp.net-web-api - WebApi Controller 获取操作不起作用

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

我的一个 WebApi2 Controller 出现问题。在编写测试时,我发现 Get() 永远不会被击中,而是返回 301,然后返回 403。奇怪的是,如果我击中 Get(id),第二个操作就会说话并完成其工作,但我永远无法击中 Get()行动。如果我重命名 Controller ,它可以正常工作,但遗憾的是,我无法按照我的意愿将 ModelsController 重命名为 ModelController,因为现有用户群期望使用该名称。我认为这是在 MVC2 中完成的现有 Api 的重写。所有其他 Controller 都工作得很好,只是这个 Controller 不行。

关于如何调试这个有什么想法吗?或者我可能错过了什么?

其他说明: 路由配置是默认的。 没有找到其他 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 }
            );
        }
    }
}

最佳答案

查看您的路由配置,存在一些问题

  1. 限制路线并不是真正必要的
  2. 在路线模式中间可选的 id 并没有真正起作用,您的可选项目应该出现在末尾。

关于asp.net-web-api - WebApi Controller 获取操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23162120/

相关文章:

azure - 通过 Web API 将文件上传到 azure 文件存储

asp.net-web-api2 - 如何使用ajax发布到另一个域

c# - 如何避免在每个 Controller 中重复一个参数?

asp.net-mvc - 使用 ASP.NET Web API 进行跨平台身份验证

asp.net-web-api - HttpControllerDispatcher 中的异常

rest - 复合键资源REST服务

c# - 返回 HttpResponseMessage 并且配置了 Application Insights 时,WebAPI 响应永远不会完成

c# - 简单注入(inject)器无需 BeginExecutionContextScope、Web Api 和 OWIN 即可工作

asp.net - 使用 asp.net 核心 API 从外部 API 获取数据

c# - ASP WebAPI 可以有可选的 RoutePrefix 吗?