【问题标题】:WebApi Controller Get Action Doesn't WorkWebApi 控制器获取操作不起作用
【发布时间】:2014-04-18 20:50:23
【问题描述】:

我的一个 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 }
            );
        }
    }
}

【问题讨论】:

  • 请分享您的路线配置,如果您有其他任何东西,这非常简单,应该可以正常工作。我想知道您是否有其他路线可以在较大的应用程序中急切地处理此 url?名称冲突的一个简单解决方法是使用属性路由
  • 正如我所指出的那样,路由设置是开箱即用的......但它就是......我曾想过尝试属性路由,但它让我无休止地烧毁它t 已经工作了...也就是说,我可能不得不...再想一想...我想我撒了谎,它看起来不是很开箱即用。谢谢...
  • 是的,受约束的路线根本没有必要。默认的开箱即用路线应该可以让您到达您需要去的地方。
  • 好的,我去默认了还是有问题...
  • 我尝试了属性路由,我做了几件不同的事情,包括设置路由顺序。没有运气,接下来我像往常一样使用项目模板创建了一个全新的 WebApi……然后我创建了一个名为 ModelsController 的控制器。接下来,我像往常一样调整了默认 routeTemplate... routeTemplate: "api/{controller}/{id}", to routeTemplate: "{controller}/{id}",问题现在在开箱即用的 webapi 中重新创建为嗯......所以是 MVC 这样做......通常我会完全删除 MVC,但这次我把它留给了 HelpPages......有人想过如何绕过它吗?

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    遵循以下任一安排(注意方法名称中的“模型”和“模型”):

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      相关资源
      最近更新 更多