[AttributeUsage(AttributeTargets.Method)]
public class AuthorizeAttribute : ActionFilterAttribute
{
/// <summary>
/// 功能項目
/// </summary>
public FunctionEnum Function { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
將要做的事情寫在這邊
base.OnActionExecuting(filterContext);
}
}
套用方式
MVC controller
[FunctionAuthorize(Function = FunctionEnum.Bank)]
public ActionResult Bank()
{
return this.View();
}
attribute後面帶進去的就是 field
2018年5月11日 星期五
2017年11月22日 星期三
2016年9月1日 星期四
更改網頁TextBox內的值,但抓取物件時還是得到更改前的值
這個問題好像一不注意就會很常出現,所以只好來個記錄了(雖然是笨問題)
我的頁面有一個DataList,然後頁面會抓資料庫中的值來進行修改
接著我在按下存檔時,動作是進到DataList的ItemCommand事件
但是我抓到的textbox的文字值一直是存檔之前的值
後來上網找了一下,發現是個很笨的錯誤
原來按下按鈕之後,首先會進入的是pageload而不是直接進入ItemCommand
而我在pageload就是做從資料庫讀取的動作,所以再存檔時抓的值一直是錯誤的
解決方式很簡單,就是在pageload讀資料庫的時候,先判斷是不是postback就好
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { GetQA(); } }
2016年7月28日 星期四
C#的webservice回傳json時結尾會多出{"d":null}
如果在webservice中
設定this.Context.Response.ContentType = "application/json";
會將回傳格式設定為json,而如果沒有回傳值的話,會預設回傳{"d":null}
修正問題方式
JsonSerializerSettings s = new JsonSerializerSettings() ;
string ss = JsonConvert.SerializeObject(bnbLogin, s);
//輸出json格式
this.Context.Response.Clear();
this.Context.Response.ContentType = "application/json";
this.Context.Response.AddHeader("content-length", ss.Length.ToString());
this.Context.Response.Flush();
//輸出json格式
this.Context.Response.Write(JsonConvert.SerializeObject(bnbLogin));
HttpContext.Current.ApplicationInstance.CompleteRequest();
重點是中間的content-length,將Response.Write的字串大小固定,就不會多傳{"d":null}了
--
補充:content-length是POST資料的長度,如果傳的內容包含中文時會因為編碼的關係而可能發生錯誤,將
this.Context.Response.AddHeader("content-length", ss.Length.ToString());
length修改為依照所編碼的字串長度,像我的Server使用UTF8編碼就修改如下:
this.Context.Response.AddHeader("content-length", System.Text.Encoding.UTF8.GetBytes(ss).Length.ToString());
設定this.Context.Response.ContentType = "application/json";
會將回傳格式設定為json,而如果沒有回傳值的話,會預設回傳{"d":null}
修正問題方式
JsonSerializerSettings s = new JsonSerializerSettings() ;
string ss = JsonConvert.SerializeObject(bnbLogin, s);
//輸出json格式
this.Context.Response.Clear();
this.Context.Response.ContentType = "application/json";
this.Context.Response.AddHeader("content-length", ss.Length.ToString());
this.Context.Response.Flush();
//輸出json格式
this.Context.Response.Write(JsonConvert.SerializeObject(bnbLogin));
HttpContext.Current.ApplicationInstance.CompleteRequest();
重點是中間的content-length,將Response.Write的字串大小固定,就不會多傳{"d":null}了
--
補充:content-length是POST資料的長度,如果傳的內容包含中文時會因為編碼的關係而可能發生錯誤,將
this.Context.Response.AddHeader("content-length", ss.Length.ToString());
length修改為依照所編碼的字串長度,像我的Server使用UTF8編碼就修改如下:
this.Context.Response.AddHeader("content-length", System.Text.Encoding.UTF8.GetBytes(ss).Length.ToString());
訂閱:
文章 (Atom)