コード例 #1
0
ファイル: CApi.cs プロジェクト: ITxiaozhang/ApiDoc3.0
        private List <string> CreateApiHtml(ApiData apiData)
        {
            var resultList = new List <string>();

            try
            {
                var dic = new Dictionary <string, object>()
                {
                };
                var backDataList = apiData.BackDataList ?? new List <FiledsData>();
                var jsonHelper   = new JavaScriptSerializer();
                apiData.EntityNullToStr <ApiData>();//对象中的string类型空值转成""

                #region 读取配置的返回格式数据
                var data = this.GetFiledsDataList(this.GetResponseData().GetType());
                var i    = 0;
                foreach (var item in data)
                {
                    if (item.Name == "Data" && !dic.Any(x => x.Key == "Data"))
                    {
                        dic.Add(item.Name, apiData.BackSuccess);
                    }
                    else
                    {
                        dic.Add(item.Name, item.Value);
                    }
                    item.ZIndex = 0;
                    backDataList.Insert(i, item);
                    i++;
                }
                #endregion
                var html          = File.ReadAllText(CApiConfig.templateUrl);
                var paramHtml     = GetFiledsHtml(apiData.ParamDataList, 1);
                var backparamHtml = GetFiledsHtml(backDataList, 0, true);
                var backSuccess   = jsonHelper.Serialize(dic);
                #region 替换文本
                html = html.Replace("{{ParamDataList}}", paramHtml);
                html = html.Replace("{{BackDataList}}", backparamHtml);
                html = html.Replace("{{Name}}", apiData.Name);
                html = html.Replace("{{BackSuccess}}", backSuccess);
                html = html.Replace("{{Describtion}}", apiData.Describtion);
                html = html.Replace("{{BackDescribtion}}", apiData.BackDescribtion);
                html = html.Replace("{{ApiUrl}}", apiData.ApiUrl);
                html = html.Replace("{{Methods}}", apiData.Methods);
                html = html.Replace("{{FuncName}}", apiData.FuncName);
                #endregion

                #region 生成文件-html
                string path = CApiConfig.fileUrl;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string       fileName     = string.Format("{0}{1}.html", apiData.CName, "_" + apiData.FuncName);
                string       fileFullPath = CApiConfig.fileUrl + fileName;
                StreamWriter sw;
                if (!File.Exists(fileFullPath))
                {
                    sw = File.CreateText(fileFullPath);
                    sw.WriteLine(html.ToString());
                    sw.Close();
                }
                else
                {
                    File.WriteAllText(fileFullPath, html);
                }
                #endregion
            }
            catch (Exception ex)
            {
                resultList.Add(ex.Message);
            }
            return(resultList);
        }