繁体   English   中英

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

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

我需要构建项目,实现供应商应用程序预定义的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这样过于复杂的常见且重要的事情呢?

有一个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继承您的控制器, ApiController在上面的包中添加,以方便从web api迁移,或者本机asp.net核心Controller 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);
    }
}

原生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 });
    }
}

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

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

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

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

属性路由补充MapRoute ,而不是替换它。

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

暂无
暂无

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

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