2016年6月12日 星期日

Why IHttpActionResult

Action Results in Web API 2 The IHttpActionResult interface was introducted in Web API 2. Essentially, it defines an HttpResponseMessage factory. Here are some advantages of using the IHttpActionResult interface:
  • Simplifies unit testing your controllers.
  • Moves common logic for creating HTTP responses into separate classes.
  • Makes the intent of the controller action clearer, by hiding the low-level details of constructing the response.
other advantages:

  • Respecting to single responsibility principle: cause action methods have the responsibility of serving the HTTP requests and should not involve in creating the HTTP response messages.
  • Useful implementations already defined in the System.Web.Http.Results namely: 
    • Ok
    • NotFound
    • Exception
    • Unauthorized
    • BadRequest
    • Conflict
    • Redirect
    • InvalidModelState
    • 完整列表
  • Uses Async and Await by default.
  • Easy to create own ActionResult just by implementing ExecuteAsync method.
  • you can use ResponseMessageResult ResponseMessage(HttpResponseMessage response) to convert HttpResponseMessage to IHttpActionResult.

2016年3月21日 星期一

Entity Framework使用資料庫View並設定關聯

Entity Model除可用資料表外也可加入資料庫檢視(View),但需先正確設定View的Entity Key,如此關聯建立時才知如何對應兩邊的資料:


建立關聯時在關聯線上點兩下滑鼠,還需指定那一邊是主要(Principal)、那邊是相依的(Dependent):

2016年3月7日 星期一

Kendo UI Grid 匯出 PDF 中文變方塊解法

  • 第一階段:先讓Kendo Grid可以匯出PDF
1.在Grid上顯示匯出PDF按鈕
$("#grid").kendoGrid({
    //...
    toolbar: ["pdf"],
    pdf: {
        allPages: true
    }
}); 
2.載入 pako_deflate.min.js 啟用壓縮 PDF 功能
<script src="Kendo/js/pako_deflate.min.js"></script>
  • 第二階段:自訂中文字型
1.定義支援Unicode的TrueType中文字型,以下以DFKai-SB為例:
@font-face {
    font-family: "DFKai-SB";
    src: url("Kendo/styles/fonts/KaiU/kaiu.ttf") format("truetype");
}
2.透過CSS讓Kendo Grid(.k-grid)使用自訂的DFKai-SB字型:
.k-grid {
    font-family: "DFKai-SB";
}