简体   繁体   English

如果使用命名空间版本路由,如何在ASP.Net Core API中设置RESTful属性路由

[英]How do I set up RESTful attribute routing in ASP.Net Core API if using Namespace version routing

I am building an ASP.Net Core 1.1.0 web API. 我正在构建一个ASP.Net Core 1.1.0 Web API。

As a supporting comment, I have chosen to use Peter Lazzarino's versionrouting NuGet package to allow me to separate my controllers into different namespaces (and folders) based on version. 作为支持性的评论,我选择使用Peter Lazzarino的versionrouting NuGet软件包,以允许我根据版本将控制器分为不同的命名空间(和文件夹)。 I configured this in my Startup.cs as recommended by Peter and it builds without errors. 我按照Peter的建议在Startup.cs中对其进行了配置,并且生成时没有错误。 I am using 我在用

var apiPrefix = "api";  

in Startup.cs to allow all of my routes to start with api, as per Peter's recommendation. 根据Peter的建议,在Startup.cs中使用,以允许我所有的路由都以api开头。

I want to follow RESTful guidelines and utilize HTTP verb attributes for my methods. 我想遵循RESTful准则,并为我的方法使用HTTP动词属性。

I want my clients to be able to go to http://companyname.com/api/v1/mycontroller/status and since I have MyController located in the the folder "Controllers\\v1\\MyController.cs" in my API project, the versionrouting library should be able to find it. 我希望我的客户能够访问http://companyname.com/api/v1/mycontroller/status ,由于我的API项目中的文件夹“ Controllers \\ v1 \\ MyController.cs”中包含MyController,因此版本路由库应该能够找到它。

Within my controller, I have the following; 在我的控制器中,我有以下内容:

namespace MyProject.Controllers.v1
{
    public class MyController : Controller
    {

        // GET: ~/api/v1/mycontroller/status
        [HttpGet("/status")]
        public JsonResult Status()
        {
            return Json(new { status = "API is running" });
        }

    }
}

However, navigating to http://companyname.com/api/v1/mycontroller/status gives me a 404 error. 但是,导航到http://companyname.com/api/v1/mycontroller/status给我一个404错误。

I had tried to leave this question as a comment in the SO post How can versioning be done in ASP.NET Core Web Api , but I do not have a high enough reputation yet in StackOverflow to leave a comment. 我曾尝试在SO帖子中将此问题作为注释留待评论, 如何在ASP.NET Core Web Api中完成版本控制 ,但是我在StackOverflow中的信誉尚不足以发表评论。 :( :(

So, hopefully someone here can help me out. 因此,希望这里有人可以帮助我。

Thanks in advance for any help you can offer. 在此先感谢您提供的任何帮助。

you can use Routes Attributes on the controller as specified in the asp.net core documentation that way you can have versioning of your api but then you will have to add it to every controller to keep your code in sync, i do not know exactly where you can add it globally. 您可以按照asp.net核心文档中指定的方式在控制器上使用Routes Attributes,这样就可以对api进行版本控制,但是随后您必须将其添加到每个控制器中,以使代码保持同步,我不知道确切的位置您可以将其全局添加。 Hope this helps 希望这可以帮助

You can simply decorate your controller with: 您可以简单地用以下方法装饰控制器:

[Route("api/v1/[controller]/[action]")]
public class MyController : Controller
{

    // GET: ~/api/v1/mycontroller/status
    [HttpGet]
    public JsonResult Status()
    {
        return Json(new { status = "API is running" });
    }

}

Then your route will be: 那么您的路线将是:

http://companyname.com/api/v1/mycontroller/status 

The solution to my problem required that I do not use any Route attribute for the class (since the namespaceversioning NuGet code is building essentially a MapRoute for api/v1/mycontroller) and also not to use the forward slash in front of the HttpGet action attribute property (ie [HttpGet("Status")] instead of [HttpGet("/Status")] ). 解决问题的方法要求我不要为该类使用任何Route属性(因为命名空间版本化的NuGet代码实质上是为api / v1 / mycontroller构建MapRoute),并且也不要在HttpGet action属性前面使用正斜杠。属性(即[HttpGet(“ Status”)]而不是[HttpGet(“ / Status”)])。 Once I made that change, it started working as expected. 一旦做出更改,它就会按预期开始工作。

This particular feature wasn't supported at the time, API Versioning now supports achieving this particular setup out-of-the-box using API Version Conventions . 当时不支持此特定功能, API版本控制现在支持使用API版本约定即时实现此特定设置。

You need only configure your application and apply the route constraint in your templates. 您只需要配置您的应用程序并在模板中应用路由约束。

services.AddApiVersioning(options =>
    options.Conventions.Add( new VersionByNamespaceConvention() );

Your service now only need be defined as: 现在,您的服务仅需定义为:

[ApiVersion("1.0")]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
public class MyController : ControllerBase
{
    // GET: ~/api/v1/my/status
    [HttpGet]
    public IActionResult Status()
    {
        return Ok(new { status = "API is running" });
    }
}

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

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