時(shí)間:2023-05-23 16:57:02 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-05-23 16:57:02 來(lái)源:網(wǎng)站運(yùn)營(yíng)
夢(mèng)想家裝平臺(tái)開(kāi)發(fā)記錄,Asp.Net Core上手實(shí)踐:Model
: 數(shù)據(jù)模型,一個(gè)普通的C#類DbContext
: 與數(shù)據(jù)庫(kù)溝通的橋梁,一個(gè)數(shù)據(jù)庫(kù)對(duì)應(yīng)一個(gè)DbContext
EF Core
服務(wù)services.AddEntityFrameworkSqlite() .AddDbContext<MainContext>(options => options.UseSqlite(Configuration["database:connection"]));
DbContext
public class MainContext : DbContext{ public MainContext() { } public MainContext(DbContextOptions<MainContext> options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json"); var configuration = builder.Build(); optionsBuilder.UseSqlite(configuration["database:connection"]); }}
這里我遇到一個(gè)很奇怪的問(wèn)題,單純?cè)?code>Startup.cs里面注冊(cè)EFCore根本不行,運(yùn)行的時(shí)候老是提示我No database provider
,只能在DbContext
里面再重寫這個(gè)OnConfiguring
,重新配置一遍數(shù)據(jù)庫(kù)= =...dotnet ef migrations add InitialCreate -v
查看狀態(tài):dotnet ef migrations list
應(yīng)用遷移來(lái)更新數(shù)據(jù)庫(kù):dotnet ef database update -v
Install-Package Swashbuckle.AspNetCore
using Swashbuckle.AspNetCore.Swagger;// 在Startup.ConfigureServices中配置服務(wù)services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo {Title = "My Api", Version = "v1"}); });// 在Startup.Configure中配置中間件app.UseSwagger();app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); });
把應(yīng)用的根路徑設(shè)置為Swagger UI
,如下:app.UseSwaggerUI(c =>{ c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); c.RoutePrefix = string.Empty;});
http://localhost:<port>/swagger/v1/swagger.json
,生成的描述終結(jié)點(diǎn)的文檔顯示如下json格式。http://localhost:<port>/swagger
找到 Swagger UI。 通過(guò) Swagger UI 瀏覽 API文檔。Controller
的方法都需要標(biāo)注出具體的Http
方法,不然會(huì)報(bào)錯(cuò)Swagger
會(huì)自動(dòng)讀取每個(gè)接口函數(shù)的C#文檔,但是前提是要生成xml文檔才可以services.AddSwaggerGen(c =>{ c.SwaggerDoc("v1", info: new OpenApiInfo { Version = "v1", Title = "Dreaming Home 智能家裝平臺(tái)", Description = "智能家裝平臺(tái) Api 文檔", TermsOfService = new Uri("http://blog.deali.cn"), Contact = new OpenApiContact { Name = "DealiAxy", Email = "dealiaxy@gmail.com", Url = new Uri("https://zhuanlan.zhihu.com/deali"), }, License = new OpenApiLicense { Name = "GNU GENERAL PUBLIC LICENSE Version 2", Url = new Uri("https://www.gnu.org/licenses/old-licenses/gpl-2.0.html"), } });});
顯示接口的xml文檔要先生成,然后在services.AddSwaggerGen
里面設(shè)置才行。// 為 Swagger JSON and UI設(shè)置xml文檔注釋路徑//獲取應(yīng)用程序所在目錄(絕對(duì),不受工作目錄影響,建議采用此方法獲取路徑)var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);var xmlPath = Path.Combine(basePath, "Doc", "DreamingHome.xml");c.IncludeXmlComments(xmlPath);
<summary>
元素的內(nèi)部文本作為api大的注釋!Get
操作方法文檔。 它可以補(bǔ)充 <summary>
元素中指定的信息,并提供更可靠的 Swagger UI。 <remarks>
元素內(nèi)容可包含文本、JSON 或 XML。 代碼如下:/// <summary> /// 這是一個(gè)帶參數(shù)的get請(qǐng)求 /// </summary> /// <remarks> /// 例子: /// Get api/Values/1 /// </remarks> /// <param name="id">主鍵</param> /// <returns>測(cè)試字符串</returns> [HttpGet("{id}")] public ActionResult<string> Get(int id) { return $"你請(qǐng)求的 id 是 {id}"; }
摘錄自:https://www.cnblogs.com/yanbigfeg/p/9232844.html接口使用者最關(guān)心的就是接口的返回內(nèi)容和響應(yīng)類型啦。下面展示一下201和400狀態(tài)碼的一個(gè)簡(jiǎn)單例子:
[ProducesResponseType(201)][ProducesResponseType(400)]
然后添加相應(yīng)的狀態(tài)說(shuō)明:返回value字符串如果id為空/// <summary> /// 這是一個(gè)帶參數(shù)的get請(qǐng)求 /// </summary> /// <remarks> /// 例子: /// Get api/Values/1 /// </remarks> /// <param name="id">主鍵</param> /// <returns>測(cè)試字符串</returns> /// <response code="201">返回value字符串</response>/// <response code="400">如果id為空</response> // GET api/values/2[HttpGet("{id}")][ProducesResponseType(201)][ProducesResponseType(400)]public ActionResult<string> Get(int id){ return $"你請(qǐng)求的 id 是 {id}";}
關(guān)鍵詞:實(shí)踐,記錄,平臺(tái),夢(mèng)想
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。