亲宝软件园·资讯

展开

c#代码生成URL地址 c#代码生成URL地址的示例

超然 人气:0
想了解c#代码生成URL地址的示例的相关内容吗,超然在本文为您仔细讲解c#代码生成URL地址的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:c#,url地址,c#,代码生成,下面大家一起来学习吧。

“头疼”

自己在用Angular做项目时,前端要请求后端数据时的代码如下

this.http.get("url/xxx")

这是请求一个URL地址数据最简单的代码,但是如此简单的代码还会遇到一些头疼的问题

“吃药”

为了解决这个问题,我们需要一个包含所有接口的文件

import { environment } from 'src/environments/environment';
export const WebAPI = {
  /** 授权控制器 */
  Auth: {
  	/** */
    Controller: `${environment.host}/api/Auth`,
    /** GET 获得用户 */
    GetUser: `${environment.host}/api/Auth/GetUser`,
    /** POST 登陆 */
    Login: `${environment.host}/api/Auth/Login`,
  },
}

那么请求代码就可以改成

this.http.get(WebAPI.Auth.GetUser)

但是维护这个文件是件吃力的事情,作为一个时刻想着如何偷懒的程序员,吃力的事情就让计算机去干,所以写一个代码生成工具。

工具代码

    public static class BuildWebApiToTS
    {
        public static string Build(Assembly assembly, string prefix = "api/")
        {
            List<Controller> controllers = GetApis(assembly);
            string code = CreateCode(controllers);
            return code.ToString();
        }
        
        public static void BuildToFile(Assembly assembly, string path, string prefix = "api/")
        {
            var code = Build(assembly, prefix);
            string existsCode = "";
            if (System.IO.File.Exists(path) == true)
                existsCode = System.IO.File.ReadAllText(path);

            if (existsCode != code)
                System.IO.File.WriteAllText(path, code);
        }

        #region 构造代码

        public static string CreateCode(List<Controller> controllers, string prefix = "api/")
        {
            StringBuilder code = new StringBuilder();
            code.AppendLine("import { environment } from 'src/environments/environment';");
            code.AppendLine("export const WebAPI = {");
            foreach (var coll in controllers.OrderBy(x => x.Name))
            {
                code.AppendLine($"  /** {coll.ApiComments?.Title} */");
                code.AppendLine($"  {coll.Name}: {{");
                code.AppendLine($"    Controller: `${{environment.host}}/{prefix}{coll.Name}`,");
                foreach (var action in coll.Actions.OrderBy(x => x.Name))
                {
                    code.AppendLine($"    /** {action.Type} {action.ApiComments?.Title} */");
                    code.AppendLine($"    {action.Name}: `${{environment.host}}/{prefix}{coll.Name}/{action.Name}`,");
                }
                code.AppendLine($"  }},");
            }
            code.AppendLine($"}};");
            return code.ToString();
        }

        #endregion

        #region 获得接口清单

        public static List<Controller> GetApis(Assembly assembly)
        {
            List<Controller> controllers = new List<Controller>();
            var collTypes = assembly.GetTypes().Where(x => x.GetCustomAttributes(typeof(ApiControllerAttribute), false).Count() > 0);
            foreach (var collType in collTypes)
            {
                var controller = new Controller(collType.Name.Replace("Controller", ""));
                controller.ApiComments = collType.GetCustomAttribute<ApiCommentsAttribute>();
                controllers.Add(controller);
                controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpGetAttribute), "GET"));
                controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpPostAttribute), "POST"));
                controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpPutAttribute), "PUT"));
                controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpDeleteAttribute), "DELETE"));
            }
            return controllers;
        }
        private static List<Action> GetTypeMembers(Type type, Type whereType, string saveType)
        {
            var actonTypes = type.GetMembers().Where(x => x.GetCustomAttributes(whereType, false).Count() > 0);
            List<Action> actons = new List<Action>();
            foreach (var actonType in actonTypes)
            {
                var action = new Action(saveType, actonType.Name);
                action.ApiComments = actonType.GetCustomAttribute<ApiCommentsAttribute>();
                actons.Add(action);
            }
            return actons;
        }

        public record Controller(string Name)
        {
            public ApiCommentsAttribute ApiComments { get; set; }
            public List<Action> Actions { get; set; } = new List<Action>();
        }

        public record Action(string Type, string Name)
        {
            public ApiCommentsAttribute ApiComments { get; set; }
        }

        #endregion
    }

    public class ApiCommentsAttribute : Attribute
    {
        public string Title { get; set; }
        public ApiCommentsAttribute(string title)
        {
            Title = title;
        }
    }

使用代码

#if DEBUG
	BuildWebApiToTS.BuildToFile(typeof(Program).Assembly, "ClientApp/src/app/web-api.ts");
#endif

上面代码大概的流程就是

  1. 利用反射读取程序集中包含ApiControllerAttribute特性的类
  2. 利用发射读取HttpGetAttributeHttpPostAttributeHttpPutAttributeHttpDeleteAttribute特性的方法
  3. 根据需要的格式生成代码
  4. 将代码文件写入ClientApp/src/app/web-api.ts

有了这个东西带来以下好处

  1. URL地址拼写错误问题,不存在的🎉
  2. 后端修改了接口,前端直接编译不过,立即发现问题🎉
  3. 鼠标停留,接口注释就显示了🎉

加载全部内容

相关教程
猜你喜欢
用户评论