2024年3月1日 星期五

Asp.net MVC Razor Page Url.Action編碼問題

在Razore Page的裡面使用Url.Action
在JS內會產生連結
連結怎麼點都會錯,仔細一看&被編碼了
外面包一層Html.Raw以免被編碼
這樣連結就會正常
參考資料
Url.Action puts an & in my url, how can I solve this?

2024年2月19日 星期一

c# Unix Timestamp轉換

近期專案接觸到很多API
發現目前大多數日期都是數字,查了一下是Unix Timestamp
寫了兩個Function來互轉
時間轉數字
public static double DateToDouble(DateTime? pNow = null)
{
    DateTime mNow = DateTime.Now;
    if (pNow != null && pNow.HasValue)
    {
        mNow = pNow.Value;
    }
    DateTimeOffset t = DateTimeOffset.Parse(mNow.ToString("yyyy-MM-dd HH:mm:ss"));
    return Convert.ToDouble(t.ToUnixTimeSeconds());
}
數字轉時間
  public static DateTime DoubleToDate(double? pDate = 0)
  {
      double d = 0;
      if (pDate != null && pDate.HasValue)
      {
          d = pDate.Value;
      }

      if (d > 9999999999)
          d = d / 1000;

      DateTime StartComputerFirstDay = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
      return StartComputerFirstDay.AddSeconds(d).ToLocalTime();
  }
參考資料
[C#] 時間轉換為Unix TimeStamp
Epoch (computing)
The Current Epoch Unix Timestamp

2024年1月25日 星期四

.Net Core The SSL connection could not be established, see inner exception.

在使用HttpClient時出現錯誤
The SSL connection could not be established, see inner exception.
網路上能找的解法都用上了都沒用
後來猜想是不是TLS支援度問題
所以將程式移至另一台OS較新的Server上就可以了
Server 2012 X > Server 2016 O
參考資料
The SSL connection could not be established, see inner exception.
Windows Server2012 R2不支持TLS1.3

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