日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

自动给 Asp.Net Core Api 增加 ApiVersionNeutral

發布時間:2023/12/4 asp.net 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自动给 Asp.Net Core Api 增加 ApiVersionNeutral 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自動給 Asp.Net Core Api 增加?ApiVersionNeutral

Intro

新增加一個 Controller 的時候,經常忘記在 Controller 上增加 ApiVersion ,結果就導致前端使用指定的 ApiVersion 訪問的時候就會失敗,不支持的 Api 版本。

錯誤信息如下:

{ "error": { "code": "UnsupportedApiVersion", "message": "The HTTP resource that matches the request URI 'http://localhost:5000/api/values' does not support the API version '1.2'.", "innerError": null } }

因此希望可以在沒有設置 ApiVersion 的時候也可以響應前端特定版本號的請求,而微軟提供了 ApiVersionNeutral 可以忽略版本,任意版本號均可訪問,于是就想自動給沒有設置 ApiVersion 的 Controller 自動設置 ApiVersionNeutral,下面就通過分析源碼來實現自動設置 ApiVersionNeutral

分析源代碼

Asp.Net Core ApiVersion 源碼地址:https://github.com/Microsoft/aspnet-api-versioning

使用 ApiVersion 會在注冊服務的地方注冊 ApiVersion 相關的服務

services.AddApiVersioning();

找到源碼 會發現注冊服務的時候把 mvc 默認的 ActionSelector 替換成了 ApiVersionActionSelector,然后查看 ApiVersionActionSelector 的源碼,找到了以下幾處關鍵代碼

ApiVersion 服務注冊

ApiVersionNetural

ApiVersionNeutralAttribute

ApiVersionActionSelector

ControllerApiVentionBuilder

總結如下:

如果 Controller 的 Attribute 定義的有 ApiVersionNeutralAttribute 就會忽略 ApiVersion 的限制,即使沒有使用 ApiVersion 或者使用任意一個 ApiVersion 都可以路由到 Action,都可以訪問得到,也不會出現開篇提到的錯誤。

解決方案

可以自己實現一個 IControllerModelConvention,去給沒有定義 ApiVersion 的控制器加 ApiVersionNeutralAttribute,實現代碼如下:

public class ApiControllerVersionConvention : IControllerModelConvention { public void Apply(ControllerModel controller) { if (!(controller.ControllerType.IsDefined(typeof(ApiVersionAttribute)) || controller.ControllerType.IsDefined(typeof(ApiVersionNeutralAttribute)))) { if (controller.Attributes is List<object> attributes) { attributes.Add(new ApiVersionNeutralAttribute()); } } } }

在注冊 Mvc 服務的時候,配置 MvcOptions

services.AddMvc(options => { options.Conventions.Add(new ApiControllerVersionConvention()); });

啟動項目,這時候再訪問原來因為沒有定義 ApiVersion 的控制器下的路由,這時就不會再報錯了,使用任意一個 ApiVersion 也都不會有問題了,問題解決啦~~~

擴展方法

為了方便使用,你也可以加一個擴展方法,在擴展方法里配置 MvcOptions,根據自己的需要,我覺得兩種方式都 OK 的,擴展方法示例如下:

public static class MvcBuilderExtensions { public static IMvcBuilder AddApiControllerVersion(this IMvcBuilder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } builder.Services.Configure<MvcOptions>(options=> options.Conventions.Add(new ApiControllerVersionConvention())); return builder; } }

使用的時候可以直接在 AddMvc 之后加上擴展方法就可以了

services.AddMvc() .AddApiControllerVersion();

實現源碼

源碼很簡單,主要是看源碼分析的過程,源碼地址:https://github.com/WeihanLi/AspNetCorePlayground/blob/master/TestWebApplication/Conventions/ApiControllerConvention.cs

現在 .net core 是開源的,有許多問題都可以通過查看源碼得到答案,有問題多 Google ,多看 Github 上的 issue,多看源碼,相信大部分問題都可以解決了。

End

問題解決,完美收官,最后還是要說一下,注意這個的使用情景,如果你要指定一個默認的 ApiVersion 有更好的方法,直接配置 ApiVersioningOptions 中的 DefaultApiVersion就可以了

services.AddApiVersioning(options => { options.AssumeDefaultVersionWhenUnspecified = true; options.DefaultApiVersion = ApiVersion.Default; });

如果你的 ApiVersion 不定,可能有些 Api 的 ApiVersion 會經常變,可以使用這種方式。

有問題歡迎聯系~~

總結

以上是生活随笔為你收集整理的自动给 Asp.Net Core Api 增加 ApiVersionNeutral的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。