What Is Software Design?

程式設計不是 “building software”,而是 “designing software”。

Is Design Dead?

軟體系統的設計是演進來的,不能一步到位,而是要藉由憑繁與使用者互動得到的回饋來修改系統設計。

Programming is Gardening, not Engineering

與其把程式設計比喻成蓋房子,實際上更像是園藝。

Orthogonality and the DRY Principle

所有程式設計活動其實都是維護,因為絕大部分的時間都在改code,寫一點改一點。即使是新專案,也很快需要回頭作修改。

顯示具有 ASP API架構 標籤的文章。 顯示所有文章
顯示具有 ASP API架構 標籤的文章。 顯示所有文章

2013年7月8日 星期一

ASP.net 2012 api 要跨網域方法

假設我們今天開發了一個 ASP.NET Web API 的 Web Services ,這個 Service 的主機名稱為 www.web1.com (稱A),同時我們也開發另外一個網站主機名稱為 www.web2.com (稱B),然而假設 B 網站存取 A 網站的 Web API ,所以我們使用 XMLRequest 或 jQuery 的 $.ajax 來呼叫我們的 Web API ,不幸的是這樣的溝通是會出錯的,因為瀏覽器不允許使用 AJAX 來呼叫外部的資源。

不過雖然瀏覽器為了安全起見阻止此類的呼叫,不過假設我們今天的請求是合法的呢?




從錯誤訊息中我們可以清楚看到 XMLHttpRequest 調用失敗,並且明確寫著來源不允許請求 "Access-Control-Allow-Origin",而什麼是  "Access-Control-Allow-Origin":當每次網頁伺服器在回應瀏覽器請求時,而當 HTTP Header 中有時會帶有  "Access-Control-Allow-Origin" 時,就可以做到跨網域的請求。


解決方法

 web.config 這個設定的作法比較簡單,但還是存在著風險,因為這樣的設定可以讓全世界的網站都可以來存取你這個網站的所有 Web API,就算是 Google 或 Facebook 也不可能公開所有的 API 來讓一般使用者存取

<system.webServer>
//裡面加入

 <httpProtocol>

    <customHeaders>
              <add name="Access-Control-Request-Origin" value="*"></add>
              <add name="Access-Control-Request-Headers" value="Origin,X-Requested-With,Content-Type,Accept" ></add>
             <add name="Access-Control-Request-Method" value="POST,GET,PUT,DELETE,OPTIONS" ></add>
    </customHeaders>
    </httpProtocol>  


2013年7月2日 星期二

初次認識 ASP.net 2012 api 撰寫 MVC架構

首先要先開一個 api的專案




good~! 現在可以開始建立api了

1.首先 要建立MVC架構裡面的 "M"


建立一個 Product.cs 的檔案


上面完成的,就是MVC裡面的「M」 ,Model / 資料模組

2.接下來也就是 MVC裡面的「C」,Controller / 控制器程式


新增ProductController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//宣告
using System.Net;
using System.Web.Http;
using TESTAPI.Models;
//請參考剛剛製作的Product.cs
//TESTAPI.Controllers
namespace TESTAPI.Controllers
{
    public class ProductController : ApiController 
    //注意要修改ApiController  在標頭要加入using System.Web.Http; 不是using System.Net.Http;
    {
        //
        // GET: /Product/
        Product[] products = new Product[]
        {
          new Product { productId = 1, productName = "Tomato Soup", productCategory = "Groceries", productPrice = 1 }, 
            new Product { productId = 2, productName = "Yo-yo", productCategory = "Toys", productPrice = 3.75M }, 
            new Product { productId = 3, productName = "Hammer", productCategory = "Hardware", productPrice = 16.99M } 
        };
        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }
        public Product GetProductById(int id)
        {
            var product = products.FirstOrDefault((p) => p.productId == id);
            if (product == null)
            {//找不到產品就會出現例外情況
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return product;
        }
        //public ActionResult Index()
        //{
        //    return View();
        //}
    }
}
恭喜你已經完成Controller / 控制器程式
接下來來test 看看吧
你可能會出現這個問題
解決問題如下
好了嗎?  來測試一下吧
 打入url =>  " /api/Product"
你是否成功看到xml了呢?
ya~
最後呢
如何設定我的 Web API 只傳回 JSON?
有撰寫過 ASP.NET Web API 的讀者可能會發現,每當使用 Chrome 來瀏覽前面的範例網頁時,都會自動變成 XML 格式,如下:
 ASP.NET Web API 內建支援 JSON 與 XML 兩種輸出格式,並依據瀏覽器端送出的 Accept 標頭自動決定回應的內容格式,不過也因為這點讓有些 Web API新手上路的開發人員來說似乎頗為困擾,如何讓 ASP.NET Web API 強迫回應 JSON 結果,以方便直接用任何瀏覽器都能看見 JSON 的執行結果。
good~
參考文獻: