2021年4月11日 星期日

[C#] 以程式更新app.config之AppSettings保留註解(By XDocument)

[C#] 以程式更新app.config之AppSettings保留註解(By XDocument)

更新app.config之appSettings(不保留註解)

  • 原app.config內容
    原app.config內容

  • 執行程式

    class Program
    {
        static void Main(string[] args)
        {
            // 寫入config序號
            UpdateSetting("TestSerial", "02");
        }
    
    
        /// <summary>
        /// 更新App.Config 中AppSetting 
        /// </summary>
        /// <param name="key">AppSetting Name</param>
        /// <param name="updateValue">AppSetting Value</param>
        private static void UpdateSetting(string key, string updateValue)
        {
            Configuration configuration =     ConfigurationManager.OpenExeConfiguration(    Assembly.GetExecutingAssembly().Location);
            configuration.AppSettings.Settings[key].Value = updateValue;
            configuration.Save();
        }
    }
    
  • 更新後app.config內容(註解被清除)
    更新後app.config內容(註解被清除)

更新app.config之appSettings(保留註解)

  • 原app.config內容
    原app.config內容

  • 執行程式

    class Program
    {
        static void Main(string[] args)
        {
            // 寫入config序號
            SaveSettingByXDocument("TestSerial", "02");
        }
    
        /// <summary>
        /// 新增/更新App.Config 中AppSetting 
        /// </summary>
        /// <param name="key">AppSetting Name</param>
        /// <param name="updateValue">AppSetting Value</param>
        private static void SaveSettingByXDocument(string key, string value)
        {
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
            var configPath = configuration.AppSettings.ElementInformation.Source;
    
            // 讀取config檔案
            XDocument document = XDocument.Load(configPath);
            if (document?.Root == null)
            {
                return;
            }
    
            // 取得appSettings節點
            XContainer appSettings = document.Element("configuration").Element("appSettings");
    
            // 取得指定key的節點
            XElement appSetting = appSettings.Elements("add").FirstOrDefault(x => x.Attribute("key").Value == key);
            if (appSetting != null)
            {
                appSetting.Attribute("value").Value = value;
                document.Save(configPath);
            }
            else
            {
                // 新增config設定
                appSettings.Add(new XElement("add",
                                    new XAttribute("key", key),
                                    new XAttribute("value", value)));
    
                document.Save(configPath);
            }
    
        }
    }
    
  • 更新後app.config內容(註解保留)
    更新後app.config內容(註解保留)



如有錯誤或建議,歡迎留言指教,謝謝!!
(相關內容如有侵犯隱私或著作權,請協助通知刪除,感謝)

2021年4月10日 星期六

Markdown轉Pdf時插入換頁符號

Markdown轉Pdf時插入換頁符號

Markdown轉Pdf時插入換頁符號

Markdown轉成Pdf檔案,如有換頁需求可輸入

<div style="page-break-after: always;"></div>

Markdown PDF可正常換頁


如有錯誤或建議,歡迎留言指教,謝謝!!

(相關內容如有侵犯隱私或著作權,請協助通知刪除,感謝)

2020年12月6日 星期日

[IIS] IIS ARR Rewrite 502.3 Bad Gateway

[IIS] IIS ARR Rewrite 502.3 Bad Gateway

IIS ARR Rewrite 502.3 Bad Gateway

前言

當Reverse Proxy設定Rewrite至未驗證之ssl網址時,頁面會顯示HTTP 502.3 Bad Gateway (發生安全性錯誤)

Http502.3

解決方法

  1. Win + R開啟執行,開啟regedit.exe

  2. 展開以下註冊表項

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\IIS Extensions\Application Request Routing\Parameters
    
  3. 用鼠標右鍵單擊參數,單擊新建,然後再單擊DWORD(32位)值

  4. 在"值名稱"框中,鍵入SecureConnectionIgnoreFlags,然後按Enter

  5. 雙擊SecureConnectionIgnoreFlags註冊表值,然後輸入3300

    Set SecureConnectionIgnoreFlags

    描述
    0x00001000 SECURITY_FLAG_IGNORE_CERT_CN_INVALID (默認)
    0x00002000 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
    0x00000100 SECURITY_FLAG_IGNORE_UNKNOWN_CA
    0x00000200 SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE
  6. 關閉註冊表編輯器

  7. 重啟iis

    iisreset /restart
    

參考網址

ARR:已添加對WINHTTP_OPTION_SECURITY_FLAGS的支持


如有錯誤或建議,歡迎留言指教,謝謝!!

(相關內容如有侵犯隱私或著作權,請協助通知刪除,感謝)

[.Net Core].NET Core 3.1 部屬至IIS上

[.Net Core].NET Core 3.1 部屬至IIS上

[.Net Core].NET Core 3.1 部屬至IIS上

安裝ASP.NET Core 3.1 Windows Hosting Bundle

  1. 先至官網下載ASP.NET Core 3.1 Windows Hosting Bundle

    ASP.NET Core 3.1 Windows Hosting Bundle Download

  2. 執行dotnet-hosting-3.1.10-win.exe安裝檔,打勾同意授權條款後,點選安裝

    WindowsServerHostingInstall

  3. 安裝完成後,重啟IIS

    • 停止 World Wide Web publishing 服務
      net stop was /y
      
    • 重新啟動該服務
      net start w3svc
      

發行網站

  1. 執行Publish指令

    dotnet publish -c Release
    

    dotnet publish

  2. 執行完成後,即可至專案下bin/Release/netcoreapp3.1/publish 取得發行檔案

    dotnet publish complete

    publish folder

新增站台

  1. 將發行的檔案放置於Server上

    Website folder

  2. 先於IIS 應用程式集區中新增一個應用程式集區

    Application pool

  3. 於IIS中新增網站,輸入站台名稱,並實體路徑指到剛剛放置發行檔案的資料夾中,然後指定要開啟的Port。

    Add Website

  4. 可同步確認模組內是否有成功安裝ASP.NET Core Module

    CheckModule1

  5. 如有看到AspNetCoreModuleV2即有成功安裝

    CheckModule2

瀏覽網站

瀏覽網頁,看是否能正常顯示頁面

View Website


如有錯誤或建議,歡迎留言指教,謝謝!!

(相關內容如有侵犯隱私或著作權,請協助通知刪除,感謝)

[C#] web.config 設定含有&字元

[C#] web.config 設定含有&字元

[C#] Web.Config 設定含有&字元

簡介

當在Web.config內的appSettings設定含有&的字元時,如未處理則可能會造成應用程式無法啟動,這裡記錄一下如何處理此問題。

2020年8月16日 星期日

[C#] 檔案寫入UTF8 或 UTF8 BOM

[C#] 檔案寫入UTF8 或 UTF8 BOM

[C#] 檔案寫入UTF8 或 UTF8 BOM

簡介

一開始使用靜態函式Encoding.UTF8寫入txt檔時,產生檔案為UTF8 with BOM,為了輸出UTF8 without BOM做一下筆記。

[Npm] 設定Proxy Server

[Npm] 設定Proxy Server

[Npm] 設定Proxy Server

簡介

由於公司上網時必須透過代理伺服器(Proxy Server)才能連到外部網路,而使用Nodejs也需要設定代理伺服器(Proxy Server)。

[Git] 設定Proxy Server

[Git] 設定Proxy Server

[Git] 設定Proxy Server

簡介

由於公司上網時必須透過代理伺服器(Proxy Server)才能連到外部網路,而使用Git也需要設定代理伺服器(Proxy Server)。

2020年8月15日 星期六

[C#] 西元年轉換為民國年

[C#] 西元年轉換為民國年

[C#] 西元年轉換為民國年

簡介

由於在維護舊程式,發現還有使用西元年減掉1911來取得民國年,這裡稍微紀錄一下修改方式。

[Oracle] 取得N~M筆資料或Top N資料

[Oracle] 取得N~M筆資料或Top N資料

[Oracle] 取得N~M筆資料或Top N資料

簡介

之前使用SQL SERVER要取得前N筆資料只需要使用TOP N即可取得所要的結果,而且還可以取得排序後的結果,而Oracle應該如何下指令呢?

SELECT TOP 10 * FROM Northwind.Orders ORDER BY CustomerID;

2019年10月30日 星期三

[Azure] Azure Storage 發佈 Angular

[Azure] Azure Storage 發佈 Angular

Deploy Angular to the Azure Storage

簡介

上一篇提到如何在Linux環境下的 Azure App Service 部屬Angular,今天試著把部屬到Azure Storage。

2019年10月15日 星期二

[Azure] Azure Linux App Service 發佈 Angular

[Azure] Azure Linux App Service 發佈 Angular

Deploy Angular to the Azure Linux App Service

簡介

最近嘗試使用Linux環境下的 Azure App Service 部屬Angular,發現與Windows環境設定方式不太一樣,所以將做法記錄一下。

2019年8月19日 星期一

[Nodejs] npm 的 .npmrc 配置文件位置

[Nodejs] npm 的 .npmrc 配置文件位置

簡介

記錄如何取得npm 的 .npmrc 配置文件位置、如何修改npmrc 配置文件以及執行ng update發生npm ERR! 401 Unauthorized處理方式。

2019年8月14日 星期三

[Azure] Azure App Service 自訂網域(搭配網路中文)

[Azure] Azure App Service 自訂網域(搭配網路中文)

簡介

如何將網路中文購買的域名設定在Azure App Service 自訂網域上。

[Angular] 路由機制-PathLocationStrategy vs HashLocationStrategy

[Angular] 路由機制-PathLocationStrategy vs HashLocationStrategy

簡介

簡單記錄一下,如何設定Url PathLocationStrategy(不帶#)(預設)與HashLocationStrategy(帶#)。

2019年5月22日 星期三

[Docker] Azure上使用Docker建立Squid Proxy

[Docker] Azure上使用Docker建立Squid Proxy

簡介

最近剛好有需要使用到其他國家的IP,除了VPN外,也可以使用squid Proxy來處理,同時程式方面也可以透過Proxy使用HttpClient。所以在Azure上自己架了一台Web Proxy,這裡做簡單的紀錄。

[WinSCP] sftp下使用sudo上傳檔案

[WinSCP] sftp下使用sudo上傳檔案

簡介

Linux主機通常會關閉root登入功能,當使用WinSCP上傳的時候會遇到被拒絕的問題,後來找到方法也能在WinSCP使用sudo功能。

2019年5月19日 星期日

[.NET Core] 使用Big5編碼

[.NET Core] 使用big5編碼

問題

當使用 .Net Core 執行 Encoding.GetEncoding("big5")時會發生Exception

System.ArgumentException: ''big5' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.'

Encoding.GetEncoding("big5")錯誤

2019年5月17日 星期五

[MSSQL] 取得系統時間

[MSSQL] 取得系統時間

簡單記錄一下SQL Server取得系統時間的各種函式

說明
GETDATE() 取得系統時間,不包含時區資訊,回傳一個 datetime 值。
CURRENT_TIMESTAMP 取得系統時間,不包含時區資訊,回傳一個 datetime 值。
GETUTCDATE() 取得 UTC+0 的時間,不包含時區資訊,回傳一個 datetime 值。
SYSDATETIME() 取得系統時間,不包含時區資訊,回傳一個 datetime2(7) 值。
SYSUTCDATETIME() 取得 UTC+0 的時間,不包含時區資訊,回傳一個 datetime2 值。
SYSDATETIMEOFFSET() 取得系統時間,包含時區資訊,回傳一個 datetimeoffset(7) 值。

2019年2月26日 星期二

[Docker] Docker建立mssql-server-linux

[Docker] Docker建立mssql-server-linux

簡介

平時在Windows 安裝SQL Server都需要花不少時間,而試著在docker上安裝SQL Server on Linux,意外發現不用幾分鐘就完成所需要的安裝。