ASP.Net Core WebApi中的非属性路由

问题描述 投票:1回答:2

我需要构建项目,实现供应商应用程序预定义的REST API(将使用它) - 大约有数千个REST资源,其中一些操作由不同的HTTP-Verb(POST,GET,PUT,DELETE等)定义。 )。

所以,理想情况下,对于每个资源,我应该有这样的单个类:

public class SomethingController
{
  public Something Post(string name, DateTime time)
  {
     // ...
  }

  public int PostStrange(string text)
  {
     // ...
  }

  public Something Put([FromBody]Something item)
  {
     // ...
  }

  public void Delete(int id)
  {
     // ...
  }
}

在以前的版本中,我可以在注册路由时调用MapHttpRoute,从ApiController继承这样的类 - 而ASP.NET Web Api将根据需要进行...但在.NET Core中我找不到像MapHttpRoute / ApiController这样的东西。现在有路由和http-verb属性,我需要为每个类/方法明确定义所有内容:

[Route("api/[controller]")]
public class SomethingController : Controller
{
    [HttpPost]
    public Something Post(string name, DateTime time)
    {
        // ...
    }

    [HttpPost("api/[controller]/strange")]
    public int PostStrange(string text)
    {
        // ...
    }

    [HttpPut]
    public Something Put([FromBody]Something item)
    {
        // ...
    }

    [HttpDelete]
    public void Delete(int id)
    {
        // ...
    }
}

为每个成千上万的REST资源编写这个属性非常无聊且容易出错......

我在这里想念一下吗?为什么在新的和现代的ASP.NET Core中,与旧的ASP.NET相比,构建REST-Api这样过于复杂的常见且重要的事情呢?

c# asp.net-web-api asp.net-core
2个回答
2
投票

有一个nuget包Microsoft.AspNetCore.Mvc.WebApiCompatShim,其主要目标是使从web api迁移到核心更容易。它还提供了一种方法来执行基于约定的路由到您需要的操作。所以,首先安装该软件包,然后在启动时:

public void ConfigureServices(IServiceCollection services) {
    // add conventions here
    services.AddMvc().AddWebApiConventions();                
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
    app.UseMvc(routes => {
        // map one global route
        routes.MapWebApiRoute("WebApi", "api/{controller}");
    });
}

在这个小配置之后,您可以从ApiController继承您的控制器,Controller是为了方便从web api迁移而添加到上面的包中,或者是本机的asp.net核心ApiControllerpublic class SomeController : ApiController { // maps to GET /api/Some // note - no routing attributes anywhere public HttpResponseMessage Get() { return new HttpResponseMessage(HttpStatusCode.OK); } // maps to POST /api/Some public HttpResponseMessage Post() { return new HttpResponseMessage(HttpStatusCode.OK); } } 的例子:

// mark with these attributes for it to work
[UseWebApiRoutes]
[UseWebApiActionConventions]
public class TestController : Controller {
    // maps to GET /api/Test
    // no routing attributes, but two "conventions" attributes
    public IActionResult Get(string p) {
        return new ObjectResult(new { Test = p });
    }
}

原生asp.net核心控制器:

[UseWebApiRoutes]
[UseWebApiActionConventions]    
public class BaseController : Controller {

}

public class TestController : BaseController {
    // maps to GET /api/Test
    // no attributes
    public IActionResult Get(string p) {
        return new ObjectResult(new { Test = p });
    }
}

您还可以使用以下属性标记基本控制器:

Controller

如果您不是从web api迁移 - 我建议使用本机ApiControllerMapRoute具有不同的结构(类似于asp.net web api ApiController),因此没有太多理由将其用于除预期目标之外的任何其他内容(从web api迁移)。


1
投票

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing仍然在那里MapRoute

属性路由赞美qazxswpoi,而不是替换它。

显然,有很多例子放弃了关于路由的文章以简化示例。所以只需挖挖斗。

© www.soinside.com 2019 - 2024. All rights reserved.