2012年12月18日 星期二

Windows Store App 建置及部署時發生錯誤





偶爾Visual Studio 2012在部署時會發生錯誤, 如上圖
通常都是模擬器不知怎麼了
我的處理方式就是關閉模擬器,再試一次通常就沒問題了
最多是Visual Studio 2012 關閉重開, 就OK啦~

2012年12月14日 星期五

解決ASP.NET WEB API get 方法 overload 的問題

如果你想寫一支程式存取某個網頁的內容, 尤其是那些知名網站ex. facebook , google maps api ...
最簡單的方式就是使用 web api 了

web api 是一種RESTful架構, 利用 http 的 request (GET, POST, PUT, DELETE)進行資源的存取 (包含查詢, 新修刪...等)

在Microsoft的技術中早先可以使用 WCF 建構 Web API
現在在asp.net 4.5 中也可以建構web api ,
你可以參考:
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

step1:建立 mvc4 web application




step2: 選擇web api

  

















step3: ValueControllers.cs












step4: test (Ctrl+F5)




得到的結果是: ["value1","value2"]

原則上取名為GetXX的方法自動會執行HTTP-Get Method
不管你的GetXXX有幾個都是視為相同的overloads
所以如果你需要Get兩組相同組合的內容時, 就會出現問題了,
Ex.
GetById (string id) 及 GetByName(string Name)那怎麼辦呢?
這時就要使用Attributes, 細節請參考:
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection 

簡單的來說, 就是使用[HttpGet]及[ActionName]再加 Routes 的設定



step1: 設定 [HttpGet]及[ActionName]

       // GET api/values      
     public IEnumerable GetAll()
     {
         return new string[] { "value1", "value2" };
     }

        // GET api/values/5              
        public string GetById(int id)
        {
            return "value";
        }

        // GET api/values/age/10
        [HttpGet]
        [ActionName("age")]
        public IEnumerable GetByAge(int arg1)
        {
            return new string[] { "aaa", "bbb" };
        }

        // GET api/values/size/10
        [HttpGet]
        [ActionName("Size")]
        public IEnumerable GetBySize(int arg1)
        {
            return new string[] { "LL", "MM" };
        }

        // GET api/values/name/departement1/anita
        [HttpGet]
        [ActionName("name")]   
        public IEnumerable GetByName(string arg1, string arg2)
        {
            return new string[] { "department", "username" };
        }


step2: 開啟WebApiConfig.cs, 設定Routes


        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
              name: "ActionApi",
              routeTemplate: "api/{controller}/{action}/{arg1}/{arg2}" ,
              defaults: new { arg2 = RouteParameter.Optional }             
            );    //<< 加上這段

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );    

        }

示範與結果:
http://localhost:33417/api/values/
會走 DefaultApi, 進入GetAll()方法
得到 ["value1", "value2"]

http://localhost:33417/api/values/100
會走 DefaultApi, 進入GetById()方法
得到 "value1"

http://localhost:33417/api/values/age/10
會走 ActionApi, 進入GetByAge(10)方法
得到 [ "aaa", "bbb"]

http://localhost:33417/api/values/size/10
會走 ActionApi, 進入GetBySize(10)方法
得到 [ "LL", "MM"]


http://localhost:33417/api/values/name/sd/anita
會走 ActionApi, 進入GetByName("sd", "anita")方法
得到 ["department", "username"]


2012年12月1日 星期六

WCF 的 DataMember 可否序列化成JSON 格式

WCF 的 DataMember 可否序列化成JSON 格式?
可以的!

有人已經寫好範例, 就參考這個吧!

http://stackoverflow.com/questions/2086666/wcf-how-do-i-return-clean-json