這篇是因為三不五時又被抓回來寫後端搞的 , 一下子 .net framework 一下子 .net core 我都搞不太清楚在寫啥
有遇到問題就順手紀錄看看
ABP 手動建立 controller
以前沒用過 ABP framework , 有機會剛好用看看 , 它裡面有個 auto api controller 機制
只要繼承 IApplicationService
就可以自動讓 Service 去產生 api , 跟之前用 java 的 spring 有個懶人用法類似
但是沒講怎麼手動建立 , 實際上在專案底下有 XXX.XXX.HttpApi
這個方案
直接在裡面加上 controller 即可
1 | public class XXXApiController : AbpController{ |
一次 POST 多筆資料 count 永遠為零
久沒寫 web api 了 , 今天遇到一個靈異事件 , post 多筆資料 countr 永遠為零 , 可能之前多半丟單一個 DTO 上來而已
關鍵在於要加上 [FromBody] 這樣才收得到
順便一提如果是用 abp 插入的話不用特別加上 [FormBody] 就可以吃到參數 , 靈異 ~
錯誤
1 | [HttpPost( "Hello" )] |
正確
1 | [HttpPost( "Hello" )] |
Missing type map configuration or unsupported mapping.
今天炸了忘了加 auto mapper 的問題 , 筆記一下
1 | Mapping types: |
首先要知道 return 的型別為後面那個 , 像是下面這樣
1 | StationDto result = ObjectMapper.Map<Station, StationDto>( station ); |
所以要在 MapperProfile 裡面定義這樣
1 | public class StationAutoMapperProfile : Profile{ |
升級 .net 3.1 to .net 5
┏[User]
┖[~\source\repos\SportSln\SportsStore]> dotnet ef migrations add Orders
Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
- You intended to execute a .NET program:
The application ‘ef’ does not exist. - You intended to execute a .NET SDK command:
A compatible installed .NET SDK for global.json version [3.1.101] from [C:\Users\User\source\repos\SportSln\SportsStore\global.json] was not found.
Install the [3.1.101] .NET SDK or update [C:\Users\User\source\repos\SportSln\SportsStore\global.json] with an installed .NET SDK:
┏[User][xERROR]5.0.201 [C:\Program Files\dotnet\sdk]
┖[\source\repos\SportSln\SportsStore]> dotnet ef migrations add Orders\source\repos\SportSln\SportsStore]>
Build started…
Build succeeded.
Done. To undo this action, use ‘ef migrations remove’
┏[User]
┖[
修改 global.json
修改前
1 | { |
修改後
1 | { |
Project
=> Your Project Properties
=> Application
=> Target framework
=> 5.0
Manage Nuget Packages
=> Update All
怎麼取得 ControllerName or ActionName
1 | var controllerName = ControllerContext.ActionDescriptor.ControllerName; |
怎麼取得 appsetting.json 內的值
首先在建構子先注入
1 | public class LaSaiController{ |
appsetting.json
1 | "LaDiSai" : { |
簡單粗暴動態切換 DB 連線字串
先隨便開個資料庫 , 然後安裝 EF Core Power Tools 可以省事很多
1 | services.AddDbContext<TestDbContext>( |
1 | private string getConnectionString(string code) |
appsetting.json
1 | "ConnectionStrings": { |
正確取得 DB 連線字串
1 | Configuration.GetConnectionString("OOXXConnection") |
多個類別繼承同個 interface 怎麼注入 DI
1 | public void ConfigureServices( IServiceCollection services ) |
Service
1 | private readonly ITestRepository testRepository; |
後來在 .net 6 發現可以這樣寫
1 | public BeforeStationController(IEnumerable<IStationRepository> stationRepositories) |
找不到 ToXXOOAsync 非同步方法
某天低能要改寫常見的 GetAll 方法 , 讓他變成 Async , 結果一時找不到 , 其實只要引用以下命名空間就好
1 | using System.Threading.Tasks; |
怎麼讓在內網的同事也看得到你現在測試的結果
調整 launchSettings.json
內的 applicationUrl
加入 https://0.0.0.0:3001;http://0.0.0.0:3000
即可
1 | "profiles": { |
簡單指定 port 測試程式
cd 到 .net5 資料夾底下
1 | dotnet OOXX.dll --urls "http://localhost:7887" |
怎麼用 HttpClient 撈遠端資料
Startup.cs
1 | public void ConfigureServices( IServiceCollection services ) |
1 | private readonly IHttpClientFactory clientFactory; |
執行 .net core 結束後關閉惱火的 console 視窗
工具
=> 選項
=> 偵錯
=> 偵錯停止時,自動關閉主控台
Tools
=> Options
=> Debugging
=> Automatcilly close the console when debugging stops
關閉產生 xml 文件造成賭爛的 Missing XML comment for publicly visible type or member
在 csproj 萬一設定了產生 xml 文件的話 , 常常會跳這個賭爛錯誤 , 只要加上 NoWarn
區塊即可消除煩人的綠毛蟲
1 | <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> |
其他 developer 懶得看文件 , 不會用 api 的 swagger 解法
常常文件寫了一狗票 , 然後突然一個需求變更就直接 gg 了 , 所以每個人在測試時根本懶得看文件
可以考慮加入 直接做個 example 讓 user 測試比較實際
1 | Swashbuckle.AspNetCore.Filters |
Startup.cs
1 | services.AddSwaggerGen(); |
一般類別只要無腦寫 xml 註解時加入 example 區塊即可
1 | public class GG |
controller 類別
1 | /// <summary> |
如何在Repository內使用Configuration
設定注入
1 | public void ConfigureServices( IServiceCollection services ){ |
注入到 Repository
1 | private readonly IConfiguration configuration; |