最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年04月12日 08:27:23   作者:超然  
這篇文章主要介紹了c#代碼生成URL地址的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

“頭疼”

自己在用Angular做項目時,前端要請求后端數(shù)據(jù)時的代碼如下

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

這是請求一個URL地址數(shù)據(jù)最簡單的代碼,但是如此簡單的代碼還會遇到一些頭疼的問題

  • URL地址拼寫錯誤,有可能折騰半天才發(fā)現(xiàn)😑
  • 后端修改了地址沒通知到前端,測試不到位就炸了,不是所有項目都有那么規(guī)范的接口變更流程😑
  • 查看代碼的時候,看著URL地址,這個地址用途是哈?還要翻接口文檔😑

“吃藥”

為了解決這個問題,我們需要一個包含所有接口的文件

import { environment } from 'src/environments/environment';
export const WebAPI = {
  /** 授權(quán)控制器 */
  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)

但是維護(hù)這個文件是件吃力的事情,作為一個時刻想著如何偷懶的程序員,吃力的事情就讓計算機去干,所以寫一個代碼生成工具。

工具代碼

    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 構(gòu)造代碼

        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. 利用發(fā)射讀取HttpGetAttribute、HttpPostAttribute、HttpPutAttribute、HttpDeleteAttribute特性的方法
  3. 根據(jù)需要的格式生成代碼
  4. 將代碼文件寫入ClientApp/src/app/web-api.ts

有了這個東西帶來以下好處

  1. URL地址拼寫錯誤問題,不存在的🎉
  2. 后端修改了接口,前端直接編譯不過,立即發(fā)現(xiàn)問題🎉
  3. 鼠標(biāo)停留,接口注釋就顯示了🎉

以上就是c#代碼生成URL地址的方法的詳細(xì)內(nèi)容,更多關(guān)于c#代碼生成URL地址的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#實現(xiàn)自定義光標(biāo)并動態(tài)切換

    C#實現(xiàn)自定義光標(biāo)并動態(tài)切換

    這篇文章主要為大家詳細(xì)介紹了如何利用C#語言實現(xiàn)自定義光標(biāo)、并動態(tài)切換光標(biāo)類型,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-07-07
  • c#唯一值渲染實例代碼

    c#唯一值渲染實例代碼

    這篇文章主要介紹了c#唯一值渲染實例代碼,有需要的朋友可以參考一下
    2013-12-12
  • Path類 操作文件類的實例

    Path類 操作文件類的實例

    下面小編就為大家分享一篇Path類 操作文件類的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • C#中重載與重寫區(qū)別分析

    C#中重載與重寫區(qū)別分析

    這篇文章主要為大家詳細(xì)介紹了C#中重載與重寫的區(qū)別,感興趣的小伙伴們可以參考一下
    2016-02-02
  • C#連接藍(lán)牙設(shè)備的實現(xiàn)示例

    C#連接藍(lán)牙設(shè)備的實現(xiàn)示例

    本文主要介紹了C#連接藍(lán)牙設(shè)備的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • C#中通過API實現(xiàn)的打印類 實例代碼

    C#中通過API實現(xiàn)的打印類 實例代碼

    這篇文章介紹了,C#中通過API實現(xiàn)的打印類 實例代碼,有需要的朋友可以參考一下
    2013-08-08
  • C#常用的命名規(guī)則匯總

    C#常用的命名規(guī)則匯總

    這篇文章主要介紹了C#常用的命名規(guī)則,較為詳細(xì)的匯總了包括類、變量、方法、屬性等的命名規(guī)則,具有很好的參考借鑒價值,需要的朋友可以參考下
    2014-11-11
  • c#調(diào)用c語言dll需要注意的地方

    c#調(diào)用c語言dll需要注意的地方

    這篇文章主要介紹了c#調(diào)用c語言dll需要注意的地方,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • unity shader實現(xiàn)較完整光照效果

    unity shader實現(xiàn)較完整光照效果

    這篇文章主要為大家詳細(xì)介紹了unity shader實現(xiàn)較完整光照效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C# 當(dāng)前系統(tǒng)時間獲取及時間格式詳解

    C# 當(dāng)前系統(tǒng)時間獲取及時間格式詳解

    這篇文章主要介紹了C# 當(dāng)前系統(tǒng)時間獲取及時間格式詳解的相關(guān)資料,這里提供代碼實例,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下
    2016-12-12

最新評論

县级市| 石景山区| 温宿县| 漯河市| 涞水县| 桑植县| 宜阳县| 宁城县| 阜康市| 荥经县| 舞钢市| 监利县| 荔浦县| 榆林市| 社旗县| 武夷山市| 大宁县| 藁城市| 武陟县| 方正县| 壤塘县| 德令哈市| 沧州市| 马龙县| 师宗县| 峨眉山市| 南京市| 凌源市| 德州市| 渭源县| 敖汉旗| 获嘉县| 临江市| 牟定县| 阜新| 介休市| 保德县| 柯坪县| 修水县| 宜兰市| 浙江省|