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年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 - 此憑證鏈結是由不受信任的授權單位發出的。)誤

2022年10月14日 星期五

.Net Core HTTP Error 500.19 - Internal Server Error

.Net Core在IIS上的錯誤
HTTP 錯誤 500.19 – 內部伺服器錯誤
HRESULT: 0x8007000d
1.web.config有問題
2.Hosting Bundle有問題(裝完記得開機)
參考資料
開啟 IIS 網頁時出現「HTTP Error 500.19 - Internal Server Error」錯誤

2022年10月5日 星期三

Angular 建立Module+Component+Input(22)

建立Module且更新app.module.ts
ng g m news --module=app
到news module資料夾
cd src\app\news
建立component
ng g c news-list --module=news
修改news.module.ts加入exports
@NgModule({
  declarations: [
    NewslistComponent
  ],
  imports: [
    CommonModule
  ],
  exports:[NewslistComponent]
})
建立list底下的component
ng g c news-head --module=news
ng g c news-body --module=news
news-list html部分
<div id="foridis{{idx}}" *ngFor="let item of data;let idx=index">
  <app-news-head [item]="item"></app-news-head>
  <app-news-body [item]="item"></app-news-body>
</div> 

news-list ts部分
  data = [
    {
      "id": 1,
      "href": "http://www.golgle.com.tw",
      "title": "Google",
      "Content": "

Google是總部位於美國加州山景城的跨國科技公司,為Alphabet的子公司,業務範圍涵蓋網際網路廣告、網際網路搜尋、雲端運算等領域,開發並提供大量基於網際網路的產品與服務,其主要利潤來自Ads等廣告服

", "href-s": "https://www.google.com.tw" }, { "id": 2, "href": "http://tw.yahoo.com", "title": "雅虎", "Content": "

Yahoo奇摩是台灣入口網站之一,由「香港商雅虎資訊股份有限公司台灣分公司」代表營運。該網站於2001年10月雅虎台灣與奇摩站合併後成立,網址續用雅虎台灣的「tw.yahoo.com」

", "href-s": "https://tw.yahoo.com", }, { "id": 3, "href": "http://www.microsoft.com/zh-tw", "title": "微軟", "Content": "

微軟是美國的一家跨國電腦科技公司,公司於1975年由比爾·蓋茲與保羅·艾倫創立,以研發、製造、授權及提供廣泛的電腦軟體服務為主。坐標為47.642229°N 122.136934°W

", "href-s": "https://www.microsoft.com/zh-tw", } ];
news-head html部分
<div>
  <a [href]="item.href" [title]="item.id">{{ item.title}}</a><br />
  <a [href]="item['href-s']">{{ item.title}}-s</a>
</div>
news-head ts部分,@Input()重點
import { Component, OnInit ,Input} from '@angular/core';

@Component({
  selector: 'app-news-head',
  templateUrl: './news-head.component.html',
  styleUrls: ['./news-head.component.css']
})
export class NewsHeadComponent implements OnInit {
  @Input()
  item: any;

  constructor() { }

  ngOnInit(): void {
  }
}

news-body html部分
<div>
  <div [innerHTML]="item.Content">
  </div>
  <pre>
  {{item.Content|json}}
</pre>
</div>
news-body ts部分,@Input()重點
import { Component, OnInit ,Input} from '@angular/core';

@Component({
  selector: 'app-news-body',
  templateUrl: './news-body.component.html',
  styleUrls: ['./news-body.component.css']
})
export class NewsBodyComponent implements OnInit {
  @Input()
  item: any;

  constructor() { }

  ngOnInit(): void {
  }

}

參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

2022年10月4日 星期二

Angular ?.安全導覽運算子(Safe Navigation Operator)(21)

目前使用Angualr 14時好像就內含了這功能無須額外寫
html部分
<div id="foridis{{idx}}" *ngFor="let item of data;let idx=index">
  <div>
    <a [href]="item.href" [title]="item.id">{{ item.title}}</a><br />
    <a [href]="item['href-s']">{{ item.sub?.xxx}}</a><br />
    <a [href]="item['href-s']">{{ item.sub?.yyy}}</a><br />
    <div [innerHTML]="item.Content">
    </div>
  </div>
</div>
ts部分
   data = [
    {
      "id": 1,
      "href": "http://www.golgle.com.tw",
      "title": "Google",
      "Content": "

Google是總部位於美國加州山景城的跨國科技公司,為Alphabet的子公司,業務範圍涵蓋網際網路廣告、網際網路搜尋、雲端運算等領域,開發並提供大量基於網際網路的產品與服務,其主要利潤來自Ads等廣告服

", "href-s": "https://www.google.com.tw", "sub": { "xxx": "xxx1", } }, { "id": 2, "href": "http://tw.yahoo.com", "title": "雅虎", "Content": "

Yahoo奇摩是台灣入口網站之一,由「香港商雅虎資訊股份有限公司台灣分公司」代表營運。該網站於2001年10月雅虎台灣與奇摩站合併後成立,網址續用雅虎台灣的「tw.yahoo.com」

", "href-s": "https://tw.yahoo.com", "sub": { "yyy": "yyy2" } }, { "id": 3, "href": "http://www.microsoft.com/zh-tw", "Content": "

微軟是美國的一家跨國電腦科技公司,公司於1975年由比爾·蓋茲與保羅·艾倫創立,以研發、製造、授權及提供廣泛的電腦軟體服務為主。坐標為47.642229°N 122.136934°W

", "href-s": "https://www.microsoft.com/zh-tw", "sub": { "xxx": "xxx3", "yyy": "yyy3" } } ];
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

Angular |SlicePipe(20)

html部分
 <div id="foridis{{idx}}" *ngFor="let item of data|slice:1;let idx=index">
  <div>
    <p>
      {{item.Content|slice:0:20}}
    </p>
    <p>
      {{item.Content|slice:-20:-1}}
    </p>
    <p>
      {{item.Content|slice:-20}}
    </p>    
  </div>
</div>

ts部分
  data = [
    {
      "id": 1,
      "href": "http://www.golgle.com.tw",
      "title": "Google",
      "Content": "

Google是總部位於美國加州山景城的跨國科技公司,為Alphabet的子公司,業務範圍涵蓋網際網路廣告、網際網路搜尋、雲端運算等領域,開發並提供大量基於網際網路的產品與服務,其主要利潤來自Ads等廣告服

", "href-s": "https://www.google.com.tw" }, { "id": 2, "href": "http://tw.yahoo.com", "title": "雅虎", "Content": "

Yahoo奇摩是台灣入口網站之一,由「香港商雅虎資訊股份有限公司台灣分公司」代表營運。該網站於2001年10月雅虎台灣與奇摩站合併後成立,網址續用雅虎台灣的「tw.yahoo.com」

", "href-s": "https://tw.yahoo.com", }, { "id": 3, "href": "http://www.microsoft.com/zh-tw", "title": "微軟", "Content": "

微軟是美國的一家跨國電腦科技公司,公司於1975年由比爾·蓋茲與保羅·艾倫創立,以研發、製造、授權及提供廣泛的電腦軟體服務為主。坐標為47.642229°N 122.136934°W

", "href-s": "https://www.microsoft.com/zh-tw", } ];
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

Angular |JsonPipe(19)

html部分
<div id="foridis{{idx}}" *ngFor="let item of data|slice:1;let idx=index">
  <pre>
{{item|json}}
</pre>
</div>
ts部分
  data = [
    {
      "id": 1,
      "href": "http://www.golgle.com.tw",
      "title": "Google",
      "Content": "

Google是總部位於美國加州山景城的跨國科技公司,為Alphabet的子公司,業務範圍涵蓋網際網路廣告、網際網路搜尋、雲端運算等領域,開發並提供大量基於網際網路的產品與服務,其主要利潤來自Ads等廣告服

", "href-s": "https://www.google.com.tw" }, { "id": 2, "href": "http://tw.yahoo.com", "title": "雅虎", "Content": "

Yahoo奇摩是台灣入口網站之一,由「香港商雅虎資訊股份有限公司台灣分公司」代表營運。該網站於2001年10月雅虎台灣與奇摩站合併後成立,網址續用雅虎台灣的「tw.yahoo.com」

", "href-s": "https://tw.yahoo.com", }, { "id": 3, "href": "http://www.microsoft.com/zh-tw", "title": "微軟", "Content": "

微軟是美國的一家跨國電腦科技公司,公司於1975年由比爾·蓋茲與保羅·艾倫創立,以研發、製造、授權及提供廣泛的電腦軟體服務為主。坐標為47.642229°N 122.136934°W

", "href-s": "https://www.microsoft.com/zh-tw", } ];
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

2022年10月3日 星期一

Angular |DecimalPipe(18)

html部分
  <p>
    {{pi | number}}
  </p>
  <p>
    {{pi | number:'4.1-5'}}-4.1-5=整數出現四位、小數點至少一位、小數最多五位
  </p>
  <p>
    minIntegerDigits.minFractionDigits-maxFractionDigits
    <br />
    minIntegerDigits:小數點前的最小整數位數。默認為1<br />
    minFractionDigits: 小數點後的最小位數。默認為0<br />
    maxFractionDigits:小數點後的最大位數。默認值為3<br />
  </p>
ts部分
  pi: number = 3.14159265359;
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

Angular |UpperCasePipe、|LowerCasePipe(17)

html部分
<p>{{mStr|uppercase}}-uppercase(轉大寫)</p>
<p>{{mStr|lowercase}}-lowercase(轉小寫)</p>
ts部分
 mStr = "aBcDe";
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

Angular |DatePipe(16)

更多格式Angular DatePipe
html部分
日期物件
<p>{{mNow|date:'medium' }}</p>
<p>{{mNow|date:'yyyy-MM-dd' }}</p>
字串物件
<p>{{mNows|date:'medium' }}</p>
<p>{{mNows|date:'yyyy-MM-dd' }}</p>
格林威治時間+小時
<p>{{mNow|date:'yyyy-MM-dd HH:mm:ss' :'+0800'}}</p>
ts部分
  mNow = Date.now();
  mNows = '2022-09-11 12:00:12';
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網
3.Angular DatePipe

2022年9月29日 星期四

Angular [ngSwitch]、*ngSwitchCase、ng-container(14)

html部分
<h5 class="btn-primary btn" (click)="CountPlus()">[ngSwitch]</h5>
<ng-container [ngSwitch]="count % 3">
  <ng-container *ngSwitchCase="0">
    <p>餘數0</p>
  </ng-container>
  <ng-container *ngSwitchCase="1">
    <p>餘數1</p>
  </ng-container>
  <ng-container *ngSwitchCase="2">
    <p>餘數2</p>
  </ng-container>
</ng-container>
ts部分
  count = 1;
  CountPlus() {
    this.count++;
  }
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

Angular *ngFor(15)

html部分
<div id="foridis{{idx}}" *ngFor="let item of data;let idx=index">
  <div>
    <a [href]="item.href" [title]="item.id">{{ item.title}}</a><br />
    <a [href]="item['href-s']">{{ item.title}}-s</a>
    <div [innerHTML]="item.Content">
    </div>
  </div>
</div>
ts部分
 data = [
    {
      "id": 1,
      "href": "http://www.golgle.com.tw",
      "title": "Google",
      "Content": "<p>Google是總部位於美國加州山景城的跨國科技公司,為Alphabet的子公司,業務範圍涵蓋網際網路廣告、網際網路搜尋、雲端運算等領域,開發並提供大量基於網際網路的產品與服務,其主要利潤來自Ads等廣告服</p>",
      "href-s": "https://www.google.com.tw"
    },
    {
      "id": 2,
      "href": "http://tw.yahoo.com",
      "title": "雅虎",
      "Content": "<p>Yahoo奇摩是台灣入口網站之一,由「香港商雅虎資訊股份有限公司台灣分公司」代表營運。該網站於2001年10月雅虎台灣與奇摩站合併後成立,網址續用雅虎台灣的「tw.yahoo.com」</p>",
      "href-s": "https://tw.yahoo.com",
    },
    {
      "id": 3,
      "href": "http://www.microsoft.com/zh-tw",
      "title": "微軟",
      "Content": "<p>微軟是美國的一家跨國電腦科技公司,公司於1975年由比爾·蓋茲與保羅·艾倫創立,以研發、製造、授權及提供廣泛的電腦軟體服務為主。坐標為47.642229°N 122.136934°W</p>",
      "href-s": "https://www.microsoft.com/zh-tw",
    }
  ];
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網