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

2012年11月24日 星期六

如何關閉wcf service host

每次在測試 WCF Service Library 時Visual Studio 會貼心的幫我們建立Host及Test程式來測試WCF服務的功能

但是當我們自行建立Host時, 這個自動建立的Host也會被啟動而佔用了指定的Port
這時只要將WCF Service Library 專案中的WCF Opiotns 頁中的 Start WCF Service Host when debugging another project in the same solution關閉即可~

2012年11月15日 星期四

最新技術參考網址


Windows Store App: http://msdn.microsoft.com/zh-tw/windows
download: http://msdn.microsoft.com/en-us/windows/apps/br229516

Windows Azure: http://www.windowsazure.com
resources: http://www.windowsazure.com/en-us/develop/net/

Live SDK: http://msdn.microsoft.com/en-US/live/ff621310

2012年11月7日 星期三

Windows 8 鍵盤快捷鍵

Windows 8 鍵盤快捷鍵
常用工具列:Windows 鍵 + C
搜索:Windows 鍵 + Q
分享:Windows 鍵 + H
開始:Windows 鍵
裝置:Windows 鍵 + K

設定:Windows 鍵 + I
有關搜尋則還有細部的快捷鍵:
搜尋設定:Windows 鍵 + W
尋找檔案:Windows 鍵 + F
http://3c.msn.com.tw/article/1210300058/1

2012年11月1日 星期四

學會Windows 8不難~


Windows 8 操作12字口訣

觸控四邊, 滑鼠四角, 右鍵無敵

詳見:

http://events.tw.msn.com/windows/windows-8-%e6%93%8d%e4%bd%9c12%e5%ad%97%e5%8f%a3%e8%a8%a3

2012年6月18日 星期一

在Windows 8 找不到你的程式嗎?


以前(windows 7)可以從 開始 -> 執行, 啟動某個應用程式,
像是 “notepad"
現在 [開始] 選單不見了, 讓人不知所措,
其實, 只要滑鼠移到時間的右側, 就會看到 [搜尋],
直接在上頭輸入要找的應用程式名稱就可以了!!





2012年6月1日 星期五

Windows 8 不會關機嗎?

[Windows] + [C] 叫出charms -> 設定 -> 開關機