我正在将 ASP.NET Core API 项目从 v5 升级到 v6。
v5中的服务配置:
services.AddSwaggerGen();
Run Code Online (Sandbox Code Playgroud)
v6中的服务配置:
builder.Services.AddEndpointsApiExplorer(); // what is this?
builder.Services.AddSwaggerGen();
Run Code Online (Sandbox Code Playgroud)
什么是AddEndpointsApiExplorer?无论我是否添加,一切都会按预期进行。
我使用“ASP.NET API 版本控制”库。他们有关系吗?如果是这样,我必须同时使用两者,只使用库,还是现在不需要库?
c# swashbuckle asp.net-core aspnet-api-versioning asp.net-core-6.0
我正在使用 Net 6 Minimal API:
application.MapGet("countries", async (IService service) => {
var countries = await mediator.Send(request);
return Results.Ok(countries);
});
Run Code Online (Sandbox Code Playgroud)
是否可以将 AspNet Api 版本控制与 Net 6 Minimal API 一起使用?如何?
我在一个项目中使用 NET 7,并且有一个关于使用哪个包进行版本控制 API 控制器的问题。有两个包 Microsoft.AspNetCore.Mvc.Versioning 和 Asp.Versioning.Http 哪一个更适合 ASP.NET Core 7?
“Microsoft.AspNetCore.Mvc.Versioning”包的版本为5.1.0,我相信它适用于.NET 5,而“Asp.Versioning.Http”包的版本为7.0.0,似乎使用第二个包是更优选的。但文档显示第二个包也用于 ASP.NET,这让我很困惑,似乎这个包是从 .NET Framework 过渡到 .NET Core 所必需的
我现在挣扎了好几天,我正在尝试使用 url 后缀实现我的 API 版本。fe http://localhost/api/v1/sites & http://localhost/api/v2/sites。
我猜我很接近,但突然间我把这一切搞砸了..
非常感谢你帮助我。
现在我收到:“System.invalidOperationException:'映射到约束键'apiVersion'的约束类型'ApiVersionRouteConstraint'必须实现IhttpRouteConstraint接口。' 在 Global.asax.cs
Global.asax.cs
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace HIDDEN
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
Run Code Online (Sandbox Code Playgroud)
WebApiConfig.cs
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Owin.Security.OAuth;
using System.Web.Http;
using System.Web.Http.Routing;
namespace HIDDEN
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only …Run Code Online (Sandbox Code Playgroud) 我正在考虑向我们现有的 API 添加版本控制。我们将版本嵌入到 URL 中。
版本控制的要求是应该很容易添加新版本,但并不是所有内容都会改变,我们不想在获得新版本时遍历所有控制器并添加新版本属性。有没有办法告诉 Microsoft.AspNetCore.Mvc.Versioning 无论版本如何,特定方法都应该可用?
我尝试使用 version:apiVersion 和基本路线来装饰我的方法。
[Route("v{version:apiVersion}/customers/{customerId}/estates/{estateId:int}/meters/{meterNumber}-{installationId}/consumption/detail")
[Route("customers/{customerId}/estates/{estateId:int}/meters/{meterNumber}-{installationId}/consumption/detail")]
Run Code Online (Sandbox Code Playgroud)
我的配置是这样的(版本号3仅用于测试目的):
services.AddApiVersioning(config =>
{
config.DefaultApiVersion = new ApiVersion(3, 0);
config.AssumeDefaultVersionWhenUnspecified = true;
config.ReportApiVersions = true;
});
Run Code Online (Sandbox Code Playgroud)
使用此配置调用端点时,我得到“与请求 URI 'http://{{host}}/api/customers/{customer}/estates/{estate}/meters/{meter}-{ 匹配的 HTTP 资源”不支持安装}/消费/详情'
一旦我添加 [ApiVersion("3.0")] 属性,一切都会正常。但我的想法是,如果我当前在版本 2 上运行,但对 API 其他部分的更改需要新版本和 API 的默认版本,我不想必须使用此控制器并“升级”版本。它应该继续响应,除非我指定特定的内容。
如果我将 AssumeDefaultVersionWhenUnspecified 更改为 false,我会收到“需要 API 版本,但未指定”。但我预计它只会抢没有版本的路线?
我在这里阅读了有关限制的信息:https://github.com/Microsoft/aspnet-api-versioning/wiki/Known-Limitations#url-path-segment-routing-with-a-default-api-version 但事实并非如此似乎不起作用。
我正在尝试将版本控制 (Microsoft.AspNet.WebApi.Versioning) 与 dotnet 5 一起用于 webapi 项目。我已经添加了 nuget 包,但出现以下错误:
“IServiceCollection”不包含“AddApiVersioning”的定义,并且找不到接受“IServiceCollection”类型的第一个参数的可访问扩展方法“AddApiVersioning”(您是否缺少 using 指令或程序集引用?)
在这一行:
services.AddApiVersioning();
Run Code Online (Sandbox Code Playgroud)
在我的 Startup.cs 中
我的 .csproj 文件如下所示:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Identity.Web" Version="1.0.0"/>
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.0.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3"/>
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.1"/>
<PackageReference Include="Dapper" Version="2.0.35"/>
<PackageReference Include="Azure.Storage.Blobs" Version="12.4.1"/>
<PackageReference Include="Microsoft.AspNet.WebApi.Versioning" Version="4.1.1"/>
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
我尝试遵循一些示例,例如这个、这个和这个,我应该有以下“使用指令”,我有:
using Microsoft.AspNetCore.Mvc;
Run Code Online (Sandbox Code Playgroud)
有人面临同样的挑战吗?.net 5 是否支持版本控制?
你好,我有一个控制器,我想对其路由进行版本控制。但是,我不希望将 api 版本添加到查询字符串中,而是添加到 url 的开头、控制器名称之后:
典型路径: /api/admin/get-computer/1
版本化路径: /api/admin/V[version]/get-computer/1
我不明白如何配置控制器和UseMvc扩展Startup:
[ApiVersion("1.0")]
[ApiVersion("2.0")]
[Route("/api/admin")]
public class AdminController : Controller {
[HttpGet]
[MapToApiVersion("1.0")]
[Route("/get-computer")]
public async Task<ActionResult<Catalog>> GetComputerAsync1(int id) {
return null;
}
[HttpGet]
[MapToApiVersion("2.0")]
[Route("/get-computer")]
public async Task<ActionResult<Catalog>> GetCatalogAsync2(int id) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
启动
public void Configure(IApplicationBuilder app) {
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Admin}/V[???]/{action=Index}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud)
首选用途
"/api/admin/V1/get-computer/3"
"/api/admin/V2/get-computer/4"
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?扩展中路由模板的假定形式是什么UseMvc?
asp.net-core ×5
c# ×3
.net ×1
.net-6.0 ×1
.net-core ×1
asp.net ×1
minimal-apis ×1
rest ×1
routes ×1
swashbuckle ×1