Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重新安装:版本化路由_C#_Asp.net Core_.net Core_Asp.net Core Webapi_Refit - Fatal编程技术网

C# 重新安装:版本化路由

C# 重新安装:版本化路由,c#,asp.net-core,.net-core,asp.net-core-webapi,refit,C#,Asp.net Core,.net Core,Asp.net Core Webapi,Refit,我们在您的一个API上使用Refit来创建和共享该API的客户端包 ICategoryApi.cs [Post("/category")] Task CreateCategoryAsync([Body] CategoryCommandDto createCategoryCommandDto); 有了这样的控制器,一切都很好 CategoryController.cs [ApiController] [Route("[controller]")] public class Categorie

我们在您的一个API上使用Refit来创建和共享该API的客户端包

ICategoryApi.cs

[Post("/category")]
Task CreateCategoryAsync([Body] CategoryCommandDto createCategoryCommandDto);
有了这样的控制器,一切都很好

CategoryController.cs

[ApiController]
[Route("[controller]")]
public class CategoriesController : ControllerBase
{
    [HttpPost]
    [ProducesResponseType((int)HttpStatusCode.Created)]
    [ProducesResponseType((int)HttpStatusCode.BadRequest)]
    public async Task<IActionResult> CreateCategory([FromBody] CategoryCommandDto createCategoryCommandDto)
    {
          //some code
    }
}

假设客户机更大,并且有很多方法,而不仅仅是一种。此外,并非所有方法都会在版本之间发生变化。

您可以通过不同的方式实现这一点: 1) 像方法中的参数一样使用

ICategoryApiV1.cs

[Post("/{v}/category")]
Task CreateCategoryAsync([Body] CategoryCommandDto createCategoryCommandDto, [AliasAs("v")]int version = 1);
2) 在
类别commanddto
中定义属性

public class CategoryCommandDto
{
    // Properties can be read-only and [AliasAs] isn't required
    public int v { get { return 1; } }
      .....
}
3) 在创建过程中为httpClient定义baseUrl

services.AddRefitClient<ICategoryApi>()
    .ConfigureHttpClient(c => c.BaseAddress = new Uri($"https://api.example.com/{version}"));
services.addClient()
.ConfigureHttpClient(c=>c.BaseAddress=新Uri($)https://api.example.com/{version}”);
4) 或者,如果您需要一些高级计算,您可以添加一个定制的HttpHandler并在您的客户端中进行配置

services.AddRefitClient<ICategoryApi>(settings)
        .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"));
        .AddHttpMessageHandler<VersionHandler>()
services.addClient(设置)
.ConfigureHttpClient(c=>c.BaseAddress=新Uri(“https://api.example.com"));
.AddHttpMessageHandler()

谢谢,如果我选择第一个选项,我是否必须修改控制器的签名并在其中添加参数?或者它只适用于您,您只需将其添加到ICategoryApiV1即可
services.AddRefitClient<ICategoryApi>(settings)
        .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"));
        .AddHttpMessageHandler<VersionHandler>()