In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use .NET 6 to achieve dynamic API, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
ApiLite is based on. Net 6 to generate dynamic api routing directly from the Service layer, which can support plug-in modules without adding Controller, which can improve work efficiency and reduce the amount of code in project development.
Development environment
.NET SDK 6.0.100-rc.2.21505.57
VS2022 Preview 7.0
Project address
GitHub: https://github.com/known/ApiLite
Project goal
Dynamically generate api according to Service
Support for custom routing templates (defined by the Route feature)
Support module plug-in
Support routing of different modules with the same Service name (namespaces need to have more than 3 levels, for example: Com.Mod.XXX)
Automatically judge the request mode according to the method name. The method that begins with Get is called GET request, and the other is POST request.
Coding convention
The module class library must contain classes that inherit the IModule interface
The Service that needs to generate api must inherit the IService interface
The method requested by GET must start with Get
Core code
It is mainly two custom classes, ApiFeatureProvider and ApiConvention, to dynamically generate api,ApiFeatureProvider inheritance ControllerFeatureProvider, override the IsController method, and determine whether the service type conforms to Controller. ApiConvention implements the IApplicationModelConvention interface and dynamically adds Action. The following is the main code. The complete code can be downloaded on GitHub.
Static class ServiceExtension {internal static WebApplicationBuilder AddKApp (this WebApplicationBuilder builder, Action? Action = null) {var option = new AppOption (); action?.Invoke (option); AddDynamicApi (mvcBuilder, option); / / add dynamic api return builder;} private static void AddDynamicApi (IMvcBuilder builder, AppOption option) {builder.ConfigureApplicationPartManager (m = > {m.ApplicationParts.Add (new AssemblyPart (typeof (IService) .Assembly)); foreach (var item in option.Modules) {item.Initialize () / / initialize the module / / add the module to ApplicationParts so that you can find the service class var assembly = item.GetType () .Assembly; m.ApplicationParts.Add (new AssemblyPart (assembly));} m.FeatureProviders.Add (new ApiFeatureProvider ());}) Builder.Services.Configure (o = > {o.Conventions.Add (new ApiConvention ());});}} / / determine whether the service type is Controllerclass ApiFeatureProvider: ControllerFeatureProvider {protected override bool IsController (TypeInfo typeInfo) {if (! typeof (IService) .IsAssignableFrom (typeInfo) | |! typeInfo.IsPublic | | typeInfo.IsAbstract | | typeInfo.IsGenericType) return false Return true;}} class ApiConvention: IApplicationModelConvention {public void Apply (ApplicationModel application) {foreach (var controller in application.Controllers) {var type = controller.ControllerType; if (typeof (IService) .IsAssignableFrom (type)) {ConfigureApiExplorer (controller); ConfigureSelector (controller) }. / / construct the routing template private string GetRouteTemplate (ActionModel action) {if (action.Attributes! = null & & action.Attributes.Count > 0) {foreach (var item in action.Attributes) {if (item is RouteAttribute attribute) {return attribute.Path / / return custom route} var routeTemplate = new StringBuilder (); / / routeTemplate.Append ("api"); var names = action.Controller.ControllerType.Namespace.Split ('.') If (names.Length > 2) {/ / support the same class name for different modules. Add the namespace module name as prefix routeTemplate.Append (names [^ 2]);} / / Controller var controllerName = action.Controller.ControllerName; if (controllerName.EndsWith ("Service")) controllerName = controllerName [0.. ^ 7] RouteTemplate.Append ($"/ {controllerName}"); / / Action var actionName = action.ActionName; if (actionName.EndsWith ("Async")) actionName = actionName [.. ^ "Async" .length]; if (! string.IsNullOrEmpty (actionName)) routeTemplate.Append ($"/ {actionName}"); return routeTemplate.ToString () }} use sample KHost.Run (args, o = > {o.Modules.Add (new TestModule ()); / / add module}); class TestModule: IModule {public void Initialize () {}} public class TestService: IService {public string GetName (string name) {return $"Hello {name}" } public string SaveData (string data) {return $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {data}";} [Route ("api/test")] public string GetCustMethod (string id) {return id;}} the above is how to use .NET 6 to implement dynamic API. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.