C# 在实体框架Web API中为路由设置默认操作

C# 在实体框架Web API中为路由设置默认操作,c#,asp.net-mvc,asp.net-web-api,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Web Api,Asp.net Mvc Routing,在默认的get、post、put和delete方法中,我添加了更多的getter,其中一些使用参数 public class AController { // GET api/A [HttpGet] public HttpResponseMessage GetAs() { ... } // GET api/A/5 [HttpGet] public HttpResponseMessage GetA(int id) { ... } /

在默认的get、post、put和delete方法中,我添加了更多的getter,其中一些使用参数

public class AController
{   
    // GET api/A
    [HttpGet]
    public HttpResponseMessage GetAs() { ... }

    // GET api/A/5
    [HttpGet]
    public HttpResponseMessage GetA(int id) { ... }

    // GET api/A/GetThis
    [HttpGet]
    public HttpResponseMessage GetThis() { ... }

    // GET api/A/GetThat
    [HttpGet]
    public HttpResponseMessage GetThat() { ... }

    // GET api/A/GetMoreInfoAbout/5
    [HttpGet]
    public HttpResponseMessage GetMoreInfoAbout(int id) { ... }

    // PUT api/A
    [HttpPut]
    public HttpResponseMessage PutA(int id, A a) { ... }

    // POST api/A
    [HttpPost]
    public HttpResponseMessage PostA(A a) { ... }

    // DELETA api/A
    [HttpDelete]
    public HttpResponseMessage DeleteA(int id) { ... }
}
到目前为止,我所做的是为每个方法指定一个新的路由,因此我的WebApiConfig.cs被炸毁,并且随着每个新的控制器和方法,它变得越来越不可转换

我尝试了更通用的路由:

config.Routes.MapHttpRoute(
    name: "ApiById",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional },
    constraints: new { id = @"^[0-9]+$" }
);

config.Routes.MapHttpRoute(
    name: "ApiByActionId",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: null,
    constraints: new { id = @"^[0-9]+$" }
);

config.Routes.MapHttpRoute(
    name: "ApiByAction",
    routeTemplate: "api/{controller}/{action}",
    defaults: null
);

/* Default route */
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
  • 我必须将默认设置放在哪里,以便
    GET api/a
    将匹配
    GetAs()
    ,而
    POST api/a
    将匹配
    PostA(a)
  • 获取api/A/5
    相同

  • 实际上,这不是我想要的解决方案,但它仍然比手动为每个控制器添加一组路由更通用

    它基本上是为每个控制器添加两条路由。这些路由模板包括对api/[ControllerName]/{id}和api/[ControllerName]的请求,并将它们委托给相应的控制器操作

    foreach (Type t in GetTypesInNamespace(Assembly.GetExecutingAssembly(), "MyProject.Controllers")) {
        if (typeof(ApiController).IsAssignableFrom(t)) { // Make sure that this controller class is deriving from api controller
            string controllerName = t.Name.Substring(0, t.Name.LastIndexOf("Controller"));  // Remove Controller postfix from name
            config.Routes.MapHttpRoute(
                name: "ApiGet" + controllerName,
                routeTemplate: "api/" + controllerName + "/{id}",
                defaults: new { controller = controllerName, action = "Get" + controllerName },
                constraints: new { id = @"^[0-9]+$", httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
            );
    
            config.Routes.MapHttpRoute(
                name: "ApiGet" + controllerName + "s",
                routeTemplate: "api/" + controllerName + "/{id}",
                defaults: new { controller = controllerName, id = RouteParameter.Optional, action = "Get" + controllerName + "s" },
                constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
            );
        }
    }
    

    我相信您可以在这里使用属性路由。有关更多信息,请参阅下面的链接:

    虽然链接可能有用,但您应该将答案放在这里,因为更改链接会使您的答案无效。