2020/01/30

[.NET Core] 在本地使用 Try .NET

markdown ### 前言 微軟有個很棒的線上執行 .NET 程式的平台 Try .NET,可以到這裡試玩學習 [.NET In-Browser Tutorial](https://dotnet.microsoft.com/learn/dotnet/in-browser-tutorial/1) 而這個 Try .NET 我們也可以下載在本地端使用唷!自己創建 Sample Code 做為教育訓練用也不錯~ ### 環境 - Windows 10 - .NET Core 3.1 ### 本地使用 Try .NET 安裝文件 [Getting started with dotnet try](https://github.com/dotnet/try/blob/master/DotNetTryLocal.md) 首先需要 .NET Core 2.1 或 3.0 以上 SDK 版本,才能進行以下步驟 #### 安裝 dotnet-try 全域工具 ``` dotnet tool update -g dotnet-try ```  
#### 開始玩 samples 官方提供 3 種方式,本範例採用第一種最簡單的開始方式,直接使用工具的預設範例 先建立一個新的資料夾,切換至此資料夾位置輸入以下指令 ``` dotnet try demo ```
#### Try .NET demo project 開啟的專案畫面如下,裡面包含使用說明及教學
#### 常用指令 用來驗證執行的 sample code 是否能正確執行 ``` dotnet try verify ``` 有趣嗎?快載來玩玩吧! ### References: - [Try .NET](https://github.com/dotnet/try) - [Try .NET Samples](https://github.com/dotnet/try-samples)

2019/12/18

[ASP.NET MVC] Visual Studio 2019 v16.4.x 右鍵加入找不到 Area 的問題

markdown ### 環境 - Visual Studio 2019 v16.4.1 - ASP.NET MVC 5 ### 問題 更新 Visual Studio 版本後,找不到加入 MVC 的 Area 區域 ### 解決方法 功能被整合至 New Scaffolded Item,步驟如下: 在專案上按右鍵 --> Add 加入,選擇 New Scaffolded Item 選擇 MVC 下的 Area,即可找到 MVC 5 Area 突然找不到功能,小驚慌了一下 XD

2019/11/18

[ASP.NET MVC] 如何在 MVC 專案下使用 Unity.Mvc IoC 依賴注入

markdown ### 前言 在 ASP.NET Core 的環境, 預設早已支援 Dependency Injection (依賴注入) 的設計模式, 但在 ASP.NET MVC 5 以前的專案沒有這樣的功能, 本篇簡單介紹如何使用 Unity.Mvc 套件來達成依賴注入。 ### 環境 - Visual Studio 2019 16.3.9 - ASP.NET MVC 5 - .NET Framework 4.7.2 - NuGet 套件: [Unity.Mvc](https://github.com/unitycontainer/aspnet-mvc) 5.11.1 (Author: Unity Open Source Project) ### 在專案中加入 Unity.Mvc


安裝套件完成後,會自動在 App_Start 加入 2 個檔案: `UnityConfig.cs` 及 `UnityMvcActivator.cs`
基本上所有的啟動功能它都做完了, 我們只要針對 `UnityConfig.cs` 這個檔案加入我們的註冊的類型即可 例如:   如果有忘記註冊的類型, 卻被使用到了, 即會出現以下的錯誤訊息 ### 程式用法 在 constructor 使用 interface 即會自動注入對應的實體 是不是很方便呢? 前人種樹, 後人乘涼, 現在只要專注去做自己該對應的相關程式, 多餘的設定全都被妥當安排了。 不知道是不是因為它太簡單使用了, 所以專案連 README 文件都沒寫 XD。 所以寫一篇用法以備不時之需, 個人覺得這是目前用過最簡易使用的套件了, 因為幾乎無痛使用啊!! 範例程式: [UnityMvcDemo1](https://github.com/onecentlin/UnityMvcDemo1) ### 相關連結 - [Unity.Mvc](https://github.com/unitycontainer/aspnet-mvc)

2019/11/16

[EF] 專案使用 Entity Framework 6.3.0 造成無法正確執行 Migration 動作

markdown ### 環境 - VS 2019 16.3.9 - Web Application (.NET Framework) 專案 - Entity Framework 6.3.0 ### 問題 在新建立的 Web 專案中, 採用 Entity Framework 6.3.0 版本, 執行 migration 時出現 Path 為空值的錯誤 ``` Enable-Migrations : Cannot bind argument to parameter 'Path' because it is null. ``` ### 暫時解決方法 降版至 EF 6.2.0 至於修正版本, 將會在 6.4.0 時修正並釋出, 所以 6.3.0 就直接跳過不要用吧!! ### 相關連結 - [EF 6.3.0 PMC commands throw ParameterBindingValidationException when Startup Project is a Web App](https://github.com/aspnet/EntityFramework6/issues/1290)

2019/08/09

[VSCode] 解決 Windows 10 1903 開啟 VSCode 內嵌 Terminal 終端機視窗跳出問題

markdown ### 問題 在 VS Code 開啟終端機時, 命令視窗是跳出式而非內嵌在 VS Code 內 ### 環境 - Windows 10 1903 版本 - VS Code 1.36.1 ### 解決方法 開啟設定,搜尋 conpty 把選項 Terminal > Integrated: Windows Enable Conpty 勾消即可 ### 參考連結 - [Terminal is always launching externally when ConPTY is turned on](https://github.com/microsoft/vscode/issues/73790) - [Intergrated terminal is pop out](https://github.com/microsoft/vscode/issues/78670) - [How do I get around the verified bug in Windows 1903 and launch the VSCode integrated terminal?](https://stackoverflow.com/questions/56154957/how-do-i-get-around-the-verified-bug-in-windows-1903-and-launch-the-vscode-integ)

2019/06/26

[Azure] 如何在 Azure Web App 啟用 PHP 模組

markdown ### 前言 最近有一個小小的 PHP 專案,資料量不大,我想直接用 Azure 的 Web App 搭用 SQLite 可攜式資料庫做應用 開個資料庫太殺雞用牛刀,而且資料庫開下去都是要 $$ 的 在部署上去之後發現 PDOException,當然就是連不上我要用的 SQLite 資料庫啦! 那該怎麼辦呢?找方法把缺的模組補上去吧! ### 環境 Azure Web App - Windows PHP 版本 ### 解決方法 1. 先在 site 資料夾下加上 ini 及 ext 的目錄,如圖: 下載 [PHP for Windows](https://windows.php.net/download/) 對應的版本 (NTS x86 版本), 需要加的模組 *.dll 上傳至 `ext` 目錄 其實我只需要 php\_pdo\_sqlite.dll 就好,但要試就一次定位吧~ 在 `ini` 目錄增加 `extensions.ini` 檔案 `extensions.ini` 內容如下 ```ini extension=d:\home\site\ext\php_pdo_sqlite.dll extension=d:\home\site\ext\php_sqlite3.dll ``` 2. 在組態設定部分,新增應用程式設定 名稱:`PHP_INI_SCAN_DIR`
 值:`d:\home\site\ini` 儲存後,記得重新啟動網站以載入模組設定 在主控台上鍵入: `php -m` 列出模組,有看到 `pdo_sqlite` 及 `sqlite3` 就表示正確載入囉! 終於網站可以確定讀到資料,完成! ### References - [在 Azure App Service 中設定 PHP](https://docs.microsoft.com/zh-tw/azure/app-service/web-sites-php-configure)

2019/06/12

[PHP] 解決連線 MySQL 錯誤訊息亂碼顯示問題

markdown ### 問題 一直以來這亂碼問題遇到的重覆不下 N 次,後來看到 `SQLSTATE[HY000] [2002]` 的錯誤代號,可以直覺反應就是連不到資料庫 Trace 出來的錯誤訊息實際是長這個樣子的,這是因為 MySQL 預設輸出錯誤訊息的是以 UTF-8 輸出,而非二進制的字符字串值從其字符集轉換為 UTF-8 就產生像以下的編碼狀況 ``` mysqli_connect(): (HY000/2002): \xb5L\xaak\xb3s\xbdu\xa1A\xa6]\xac\xb0\xa5\xd8\xbc\xd0\xb9q\xb8\xa3\xa9\xda\xb5\xb4\xb3s\xbdu\xa1C\r\n ``` 我們看顯示的樣子就像下圖,以使用者端來看就是有點不開心 ### 解決方法 用 mb\_convert\_encoding 的方式,先將取得編碼訊息轉成 utf-8 (以繁體中文的來說就是 big5) 如此就能正常顯示中文訊息啦 ### References - [iconv big5轉換utf8問題](http://sweslo17.blogspot.com/2012/04/big5-erpms-sql-local-cache-phpiconv.html) - [PHP: mb\_convert\_encoding](https://www.php.net/manual/en/function.mb-convert-encoding.php) - [MySQL: 10.6 Error Message Character Set](https://dev.mysql.com/doc/refman/5.7/en/charset-errors.html)