2024/10/24
[ASP.NET MVC] 解決在 WebAPI 讀取不到 Session 值的問題
markdown
### 環境
- ASP.NET MVC 5
- .NET Framework 4.7.2+
### 問題
原本有個設定值以 Cookie 為存取來源, 但因應安全理由改寫成 Session 來存取, 卻在 WebAPI 存取時發生讀取不到 Session 的內容。
在 ASP.NET Core MVC 反而簡單, 只要 `services.AddSession()` 跟 `app.UseSession()` 即搞定。
目前還是有許多不是 .NET Core 的專案,記錄一下以供未來翻查用。
### 解決方法
修改 `Global.asax.cs` 檔案
```cs
protected void Application_PostAuthorizeRequest()
{
// WebApi SessionState
bool isWebApiRequest = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith("~/api");
if (isWebApiRequest) HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
```
### 參考資料
- [Accessing Session Using ASP.NET Web API](https://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api)
2024/06/18
[C#][WebAPI] Json.NET / System.Text.Json 序列化物件中忽略 null 值的屬性
markdown
近期遊走在 ASP.NET MVC 5 / Web API 2 以及 ASP.NET Core MVC (.NET) 的專案中
對於由 API 讀取進來的物件在 .NET Framework 與 .NET 下處理的方式不同, 需要記錄一下以便日後翻查
場景需求
使用場景是把接收進來的物件內容轉換做一個清洗,只要從 Web API 接進來的物件屬性值為 null 的都不保留 例如: 有一個 Log 物件如下 ```C# var log = new Log { Timestamp = "2024-06-16T09:22:01.5120098Z", Level = "Information", Message = "This is test message", Exception = null }; ``` 那麼經過清洗過後的字串內容就不會包含 Exception 空值的屬性 ```Json { "Timestamp":"2024-06-16T09:22:01.5120098Z", "Level":"Information", "Message":"This is test message" } ``` 以下即是在 ASP.NET MVC 5 及 ASP.NET Core 不同環境的處理方式2023/02/19
[.NET] 如何使用 .NET Upgrade Assistant 升級舊 ASP.NET MVC 專案
markdown
### 問題
進入 .NET 7 的今日,手邊仍有許多舊的專案,要如何將舊專案升級呢?
微軟提供了 .NET Upgrade Assistant 工具來協助升級舊專案。
讓我們一起來一步步升級吧!
### 升級專案
舊專案環境:
- ASP.NET MVC 5.2.9
- .NET Framework 4.7.2
想要升級目標專案至 .NET 7
2019/12/18
[ASP.NET MVC] Visual Studio 2019 v16.4.x 右鍵加入找不到 Area 的問題
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)



安裝套件完成後,會自動在 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/06/09
[ASP.NET MVC] 解決 DirectoryNotFoundException 找不到路徑 bin\roslyn\csc.exe 的問題
markdown
### 前言
事情發生在 clone 專案編譯執行後發生的。以前曾經也發生過,但不記錄還是會忘了解法,趁現在遇到了,快記一下!
### 問題
ASP.NET MVC 專案在 git clone 下來編譯後執行網站出現:System.IO.DirectoryNotFoundException: 找不到路徑 'bin\roslyn\csc.exe' 的一部分
### 解決方法
開啟專案 `*.csproj` 檔,加上 Target 的 AfterTargets 設定
### 參考
- [Could not find a part of the path … bin\roslyn\csc.exe](https://stackoverflow.com/questions/32780315/could-not-find-a-part-of-the-path-bin-roslyn-csc-exe)
- [找不到 roslyn\csc.exe ?!](https://blog.yowko.com/missing-roslyn-csc/)
- [Web API找不到 bin 底下的 roslyn csc.exe ?](http://marcus116.blogspot.com/2018/11/net-web-api-bin-roslyn-cscexe.html)
### 解決方法
開啟專案 `*.csproj` 檔,加上 Target 的 AfterTargets 設定
### 參考
- [Could not find a part of the path … bin\roslyn\csc.exe](https://stackoverflow.com/questions/32780315/could-not-find-a-part-of-the-path-bin-roslyn-csc-exe)
- [找不到 roslyn\csc.exe ?!](https://blog.yowko.com/missing-roslyn-csc/)
- [Web API找不到 bin 底下的 roslyn csc.exe ?](http://marcus116.blogspot.com/2018/11/net-web-api-bin-roslyn-cscexe.html)
2019/02/22
[ASP.NET MVC] 解決 SqlException 錯誤: 此憑證鏈結是由不受信任的授權單位發出的。
markdown
在佈署網站時總會遇到一些眉眉角角的問題
今天到客戶要求的主機上佈署網站,出現:「此憑證鏈結是由不受信任的授權單位發出的。」 的錯誤訊息

**訊息:[SqlException (0x80131904): 與伺服器的連接已成功建立,但在登入程序時發生錯誤。 (provider: SSL Provider, error: 0 - 此憑證鏈結是由不受信任的授權單位發出的。)]** 看起來資料庫連線沒問題,只是在登入步驟出了差錯。SSL Provider 提供重要線索,首先檢查連線字串~ 原本測試站是用 Azure 的 Database 服務,連線字串包含 Encrypt=True;TrustServerCertificate=False;,但客戶主機不適用,拿掉就可以正常連線了! 除錯任務完成,特此記錄一下!

**訊息:[SqlException (0x80131904): 與伺服器的連接已成功建立,但在登入程序時發生錯誤。 (provider: SSL Provider, error: 0 - 此憑證鏈結是由不受信任的授權單位發出的。)]** 看起來資料庫連線沒問題,只是在登入步驟出了差錯。SSL Provider 提供重要線索,首先檢查連線字串~ 原本測試站是用 Azure 的 Database 服務,連線字串包含 Encrypt=True;TrustServerCertificate=False;,但客戶主機不適用,拿掉就可以正常連線了! 除錯任務完成,特此記錄一下!
2015/02/28
[ASP.NET MVC] 網站讀取靜態 JSON 檔案權限
Web.config 加上以下設定, 以讀取 JSON 檔名的檔案
記得要加這一段~~
記得要加這一段~~
<mimeMap fileExtension=".json" mimeType="application/json; charset=UTF-8" />
2010/05/05
2010/04/27
[ASP.NET] Forms 登入驗證
命名空間
驗證
登出
查證是否驗證
ASP.NET MVC (在 Controller 的 Action 前加 Authorize 屬性)
e.g.
相關資料:
System.Web.Security驗證
FormsAuthentication.RedirectFromLoginPage(username, true)登出
FormsAuthentication.SignOut()查證是否驗證
User.Identity.IsAuthenticatedASP.NET MVC (在 Controller 的 Action 前加 Authorize 屬性)
[Authorize]e.g.
[Authorize]
public ActionResult Index() { return View(); }相關資料:
訂閱:
意見 (Atom)



