EF Core概念Model: 數(shù)據(jù)模型,一個(gè)普通的C#類DbContext: 與數(shù)據(jù)庫(kù)溝通的橋梁,一個(gè)數(shù)據(jù)庫(kù)對(duì)應(yīng)一個(gè)DbContext注冊(cè)EF Core服務(wù)service" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁(yè) > 營(yíng)銷資訊 > 網(wǎng)站運(yùn)營(yíng) > 夢(mèng)想家裝平臺(tái)開(kāi)發(fā)記錄,Asp.Net Core上手實(shí)踐

夢(mèng)想家裝平臺(tái)開(kāi)發(fā)記錄,Asp.Net Core上手實(shí)踐

時(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í)踐:

DreamingHome

Dreaming Home 夢(mèng)想家,家裝平臺(tái)

EF Core概念

注冊(cè)EF Core服務(wù)

services.AddEntityFrameworkSqlite() .AddDbContext<MainContext>(options => options.UseSqlite(Configuration["database:connection"]));

數(shù)據(jù)庫(kù)上下文 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ù)= =...

數(shù)據(jù)庫(kù)遷移

創(chuàng)建數(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

引入Swagger支持

安裝依賴

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;});

啟動(dòng)測(cè)試

啟動(dòng)應(yīng)用,并導(dǎo)航到http://localhost:<port>/swagger/v1/swagger.json,生成的描述終結(jié)點(diǎn)的文檔顯示如下json格式。

可在 http://localhost:<port>/swagger 找到 Swagger UI。 通過(guò) Swagger UI 瀏覽 API文檔。

注意

自定義以及擴(kuò)展

修改之前配置的Swagger服務(wù):

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);

小結(jié)

通過(guò)上面的操作可以總結(jié)出,Swagger UI 顯示上述注釋代碼的 <summary> 元素的內(nèi)部文本作為api大的注釋!

當(dāng)然你還可以將 remarks 元素添加到 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}"; }

描述響應(yīng)類型

摘錄自: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為空

最終代碼應(yīng)該是這個(gè)樣子:

/// <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}";}

運(yùn)行效果

歡迎交流

交流問(wèn)題請(qǐng)?jiān)谖⑿殴娞?hào)后臺(tái)留言,每一條信息我都會(huì)回復(fù)哈~

關(guān)鍵詞:實(shí)踐,記錄,平臺(tái),夢(mèng)想

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉