2023年12月4日 星期一

.Net Core Scaffold-DbContext複數化問題及不產生不產生OnConfiguring

早上寫好程式後更新Model卻發生了以下問題
Cannot create a DbSet for 'xxxMinutes' because this type is not included in the model for the context.
後來找了一下xxxMinutes多了一個xxxMinute的Class
原來的
Scaffold-DbContext "Server=192.168.0.1;Database=abc;user id=sa;password=12345678;TrustServerCertificate=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models  -Project board.model  -UseDatabaseNames -NoOnConfiguring -Force   
多加一個-NoPluralize停止複數化
多加一個-NoOnConfiguring不產生OnConfiguring
Scaffold-DbContext "Server=192.168.0.1;Database=abc;user id=sa;password=12345678;TrustServerCertificate=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models  -Project board.model  -UseDatabaseNames -NoOnConfiguring -NoPluralize -Force   
參考資料
Entity Framework Core tools reference - Package Manager Console in Visual Studio

2023年11月2日 星期四

.Net Core Resolve nullable warnings

.Net6專案常會產生Resolve nullable warnings
關掉的方法找到專案的.csproj(或右鍵編輯專案檔)
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>
修改
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>disable</Nullable>
  </PropertyGroup>
參考資料
可為 Null 的參考型別
.NET 6学习笔记(4)——解决VS2022中Nullable警告
Resolve nullable warnings

2023年10月31日 星期二

.Net Core 如何新增Area區域

1.專案右鍵 > 加入 > 新增Scaffold項目
2.MVC區域 > 加入
3.在Program.cs
 app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
加入
 app.MapControllerRoute(
    name: "MyArea",
    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
4.在Controller加入Area tag
參考資料
ASP.NET Core 中的區域

.Net Core 發佈後多了許多語言檔資料夾

.net發佈網站後可能會多了許多沒用的語言檔資料夾
找到專案的.csproj(或右鍵編輯專案檔)
 <PropertyGroup>
   <TargetFramework>net6.0</TargetFramework>
 </PropertyGroup>
修改
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
	<SatelliteResourceLanguages>en-US;zh-TW</SatelliteResourceLanguages>
  </PropertyGroup>
乾淨多了
參考資料
【笨問題】防止 .NET 編譯產生不需要的多語系資源檔
ASP.Net Core exclude published language directories other than english

2023年10月27日 星期五

c# 變更ClickOnce產生出的setup.exe的icon

使用ClickOnce會自動更新,所以有時給user時會直接給setup.exe
但icon的話就有可能跟別的重複,比較不好分辨
以下方法可修改icon
1.開啟Visual Studio
2.檔案 > 開啟 > 檔案 > 選擇setup.exe
3.icon > 加入資源
4.匯入 > 選擇icon
5.確保匯入後的編號在前面即可
PS.windows有時候不會即時變更顯示,須重開機
參考資料
【转】vs2012 打包安装更改 setup.exe的图标
How to change Setup.exe icon when publishing through clickonce

2023年10月20日 星期五

.Net Core AnonymousID

購物車常使用的技巧Request.AnonymousID
在.Net Core因為沒參考System.Web
可改用ReturnTrue.AspNetCore.Identity.Anonymous
Startup.cs加入
app.UseAnonymousId();
取得方式
  IAnonymousIdFeature feature = HttpContext.Features.Get<IAnonymousIdFeature>();
  string anonymousId = string.Empty;
  if (feature != null)
  {
      anonymousId = feature.AnonymousId;
  }
參考資料
Git Hub AnonymousId
ReturnTrue.AspNetCore.Identity.Anonymous

2023年10月17日 星期二

Asp.net 解決根目錄根子目錄應用程式衝突問題

網站越做越大的時候常會有在子目錄底下新增應用程式
這時常會遇到子目錄的web.config跟根目錄的內容有衝突
在根目錄底下新增以下程式碼可直接解決繼承問題
<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<location path="." inheritInChildApplications="false">
	</location>
</configuration>
參考資料
如何讓同一個 IIS 站台設定兩個不同的 ASP.NET 應用程式

2023年8月7日 星期一

.Net Core Shadow-copying解決發佈至IIS時dll無法覆蓋問題

.net core更新經常都需要停用應用程式集區
此時最容易造成503 service unavailable
找了幾篇文章有個功能Shadow-copying但.net6開始才有
整理了使用流程如下
1.修改專案改成.net6以上
2.修改或加入web.conifg,內容,xxxShadowCopyDirectory的部分依您路徑需求修改,xxx.dll為您會被鎖的的那個檔通常是專案名稱,若不知道可在發布後找一下
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>      
      <handlers>
	     <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="%LAUNCHER_PATH%" arguments="./xxx.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
        <handlerSettings>
          <handlerSetting name="experimentalEnableShadowCopy" value="true" />
          <handlerSetting name="shadowCopyDirectory" value="../xxxShadowCopyDirectory/" />          
        </handlerSettings>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>
3.IIS上安裝Hosting Bundle,需重新啟動Server
4.發佈專案,發佈時特別注意,如會直接發佈到IIS上資料夾,刪除現有檔案要設定為false
5.只要檔案蓋過IIS上的目錄就會自動觸發,xxxShadowCopyDirectory資料夾中就會多一個版本
6.experimentalEnableShadowCopy在.net7後變成enableShadowCopy
參考資料
ASP.​NET Core in .NET 6 - Shadow-copying in IIS
如何啟用 ASP.NET Core 6.0 部署到 IIS 的陰影複製 (Shadow-copying) 功能
.NET6 ShadowCopy
下载 .NET 6.0

2023年7月5日 星期三

React 專案放於非根目錄資料夾中解決方法

package.json加入 "homepage": "/子資料夾名稱/"

React input maxLength and only number

經過測試 type="number" maxLength會無效
目前這方法可用
<input type="text"
style={{ textAlign: "right" }}                    
maxLength={9}
onChange={onlynumber} />
const onlynumber = (e) => {
      //去掉非數字
       e.target.value = e.target.value.replace(/\D/g, '');
       //todo anything
};
參考資料
Create a Numbers only Input field in React.js

2023年2月13日 星期一

Windows Robocopy 同步資料

網路上有很多同步資料軟體,有些可能需要$$
Robocopy 內建在windows中,使用命令提示字元中執行
或寫成一個bat檔
如果需要定時執行也可設定windwos排成呼叫bat
robocopy 來源端 目的端 參數
robocopy d:\123  \\192.168.0.1\d$\123 /s /purge

robocopy 來源端 目的端 寫日期Log
robocopy d:\123  \\192.168.0.1\d$\123 /s /purge /LOG+:d:\%date:~0,4%%date:~5,2%%date:~8,2%.log
參考資料
Robocopy高速同步備份、複製、移動大量檔案
使用Windows Robocopy每日同步資料並產生log檔驗證

2023年1月18日 星期三

.Net Core Axios.Get Webapi 抓不到 404

情境
axios.get 傳第三個參數給 .Net Core WebApi,如果三個值都有時WebApi可以接收到
但如果只傳遞了兩個參數其中一個是NULL或空值時就抓不到
解決方法如下在Startup輸入以下
 services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);

2023年1月11日 星期三

.Net Core provider: SSL Provider, error: 0 - 此憑證鏈結是由不受信任的授權單位發出的

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 此憑證鏈結是由不受信任的授權單位發出的。)
連線字串有問題
Server=192.168.0.1;Database=xxdb;user id=test;password=test1234;
加入TrustServerCertificate=true
Server=192.168.0.1;Database=xxdb;user id=test;password=test1234;TrustServerCertificate=true;
參考資料
[SQL Server] A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 此憑證鏈結是由不受信任的授權單位發出的。)誤