简体   繁体   English

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

[英]Non-attribute routes in ASP.Net Core WebApi

I need to build project, that implement REST API predefined by vendor application(which will consume it) - there is about thousand of REST-resources with some actions defined by different HTTP-Verb's(POST, GET, PUT, DELETE, etc..). 我需要构建项目,实现供应商应用程序预定义的REST API(将使用它) - 大约有数千个REST资源,其中一些操作由不同的HTTP-Verb(POST,GET,PUT,DELETE等)定义。 )。

So, ideally, for each resource i should have single class like this: 所以,理想情况下,对于每个资源,我应该有这样的单个类:

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)
  {
     // ...
  }
}

In previous versions i can just call MapHttpRoute while registering routes, inherit classes like this from ApiController - and ASP.NET Web Api will do as i need... But in .NET Core i can't find anything like MapHttpRoute/ApiController.. Now there is routing and http-verb attributes, and i need to define everything explicitly for each class/method: 在以前的版本中,我可以在注册路由时调用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)
    {
        // ...
    }
}

Writing this attributes for each of thousands REST-resources is very boring and error prone... 为每个成千上万的REST资源编写这个属性非常无聊且容易出错......

Do i miss something here? 我在这里想念一下吗? Why in pretty new and modern ASP.NET Core that very common and important thing as building REST-Api made so over-complicated, compared to old ASP.NET? 为什么在新的和现代的ASP.NET Core中,与旧的ASP.NET相比,构建REST-Api这样过于复杂的常见且重要的事情呢?

There is nuget package Microsoft.AspNetCore.Mvc.WebApiCompatShim which main goal is to make migration from web api to core easier. 有一个nuget包Microsoft.AspNetCore.Mvc.WebApiCompatShim ,其主要目标是使从web api迁移到核心更容易。 It also provides a way to perform convention-based routing to actions you need. 它还提供了一种方法来执行基于约定的路由到您需要的操作。 So, first install that package, then in startup: 所以,首先安装该软件包,然后在启动时:

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}");
    });
}

After this small configuration you can inherit your controllers either from ApiController , which is added in package above for convenience of migration from web api, or native asp.net core Controller . 在这个小配置之后,您可以从ApiController继承您的控制器, ApiController在上面的包中添加,以方便从web api迁移,或者本机asp.net核心Controller Example of ApiController : ApiController示例:

public 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);
    }
}

Native asp.net core controller: 原生asp.net核心控制器:

// 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 });
    }
}

You can also mark your base controller with these attributes: 您还可以使用以下属性标记基本控制器:

[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 });
    }
}

If you are not migrating from web api - I'd suggest to use native Controller . 如果您不是从web api迁移 - 我建议使用本机Controller ApiController has different structure (similar to asp.net web api ApiController), so there is not much reason to use it for anything other than its intended goal (migration from web api). ApiController具有不同的结构(类似于asp.net web api ApiController),因此没有太多理由将其用于除预期目标之外的任何其他内容(从web api迁移)。

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

Attribute routing compliments MapRoute , not replaces it. 属性路由补充MapRoute ,而不是替换它。

Apparently there are quite a few examples which drop the piece about Routing in order to simplify example. 显然,有很多例子放弃了关于路由的文章以简化示例。 So just dig dipper. 所以只需挖挖斗。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM