[C#] 於Code Level驗證URL
有時制造表格時, 須要去驗證用戶輸入的URL是否正確, 這個可以透過Attribute 完成. 但若果得到的URL是從app.config 或其他檔案而來, 那就變得有須要在code level 進行一次檢查.
有時制造表格時, 須要去驗證用戶輸入的URL是否正確, 這個可以透過Attribute 完成. 但若果得到的URL是從app.config 或其他檔案而來, 那就變得有須要在code level 進行一次檢查.
JSON 是一種方便的serialization format. 相對於XML, 其容量相對較小, 更適合於network 中傳輸. 當要將dictionary serialize 時, 方法如下:
在.net environment, 本身不支援UNC path, 若加上user name /password 設定, 總會throw exception. 網上找來一個class, 它是call windows dll 去進行連結. 若在windows environment 的話, 不失為一個好方法.
在顯示貨幣時, 每隔三個位, 都須要加一個”,”分隔, 在C# 可以自行implement 如下: public static string ConvertToCurrency(double value) { string result = String.Format(“{0:#,##0.##00}”,value); return result; }
部份內容須要儲存或傳送, 若plain text 的話會是一個考量, 故須要將它encode. public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); } public static string Base64Decode(string base64EncodedData) { var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); }
在send email 前, 先講如何制造email object. 這裡用了一粒class 去包著須要send的內容. 故砌真. object 時, 這個method 可以定義為adapter 的implementation. private MailMessage createMailMessage(EMAIL email) { MailMessage mail = new MailMessage() { From = new MailAddress(email.FROM_EMAIL_ADDRESS), }; mail.To.Add(new MailAddress(email.TO_EMAIL_ADDRESS)); if (!string.IsNullOrEmpty(email.CC_EMAIL_ADDRESS)) mail.CC.Add(new MailAddress(email.CC_EMAIL_ADDRESS)); […]
View: 留意須要自行定義encType作multipart/form-data, 不然的話controller 不會接到file value. @using (Html.BeginForm(“CreatePost”, “Email”,FormMethod.Post, new { enctype= “multipart/form-data”, id=”formSendEmail” })) { @Html.AntiForgeryToken() <div class=”form-horizontal”> <input type=”file” id=”attachments” name=”attachments[]” multiple /> <div class=”form-group”> <div class=”col-md-offset-2 col-md-10″> <input type=”submit” value=”Create with file” class=”btn […]
若在concurrency中, 有時須要產生一堆random number去做, 但工作上, 要制做一條unique path, 除了用primary key 外, timestamp + random number 亦是一個好選擇. 為了更精準 (或者無聊), 便嘗試了random string. private string RandomString(int size) { Random random = new Random((int)DateTime.Now.Ticks); StringBuilder builder = new StringBuilder(); char ch; […]
在建構框架 (framework)時, 會有機會須要重覆使用一堆指定的 user control. 其實透過 extend HtmlHelper, 便可以將coding變得簡單, 將來修改時亦只須要修改一部份便可. 在這裡, 用了一個Bootstrap control 做例子. public static class HtmlHelperExtend { public static MvcHtmlString DateTimePicker(this HtmlHelper helper, string divDayTimePickerId, string txtBoxdayTimePickerId, DateTime? value, string labelId, string labelText=”Label […]
當session timeout時, 若不作處理, 運作時有機會throw exception. 為應對此情況, 可以自訂一個Attribute, 當controller / action call 時, 便會先行attribute. using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Test.Attributes { public class SessionExpireAttribute : ActionFilterAttribute { public override void […]
Copyright © 2026 | MH Magazine WordPress Theme by MH Themes