我的一个 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 }
);
}
}
}
最佳答案
查看您的路由配置,存在一些问题
- 限制路线并不是真正必要的
- 在路线模式中间可选的 id 并没有真正起作用,您的可选项目应该出现在末尾。
关于asp.net-web-api - WebApi Controller 获取操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23162120/