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());

2016年7月25日 星期一

webservice 上傳檔案 找不到路徑 'D:\' 的一部分

在使用webservice上傳檔案時,有出現找不到路徑 'D:\' 的一部分的錯誤
原因可能如下:
1.資料夾權限未開啟
2.路徑錯誤

個人遇到時的錯誤是第二個,路徑在建立時須包含完整資料夾路徑加檔案名稱

FileStream fileStream = new FileStream(Server.MapPath(@".\test\" + this.Context.Request.Files[0].FileName), FileMode.Create);

如果只有前面資料夾部分就會錯誤上傳失敗

2016年7月15日 星期五

為Blogger程式碼排版

參考網站 http://holeyshared.blogspot.tw/2016/05/blogger-code-highlighter.html
-- 這是看了好幾篇文章確定能用的,在這邊做個筆記 我使用的是 Google Code Prettify,進入 Blogger -> 設計 -> [版面配置],按下 [新增小工具],選擇新增 [HTML/JavaScript],然後在小工具中貼入以下程式碼:

<style type="text/css">
/* 程式碼高亮設定 */
/* Main Box */
.pre-highborder{
    border: 1px solid #ff0000;
    padding: 3px 3px 3px 0;
}
pre.prettyprint, code.prettyprint {
    border-radius: 8px;
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;
    padding: 5px;
 overflow: auto;
    background-color: #eee !important;
    color: black;
    box-shadow: 0 0 5px #999;
    -moz-box-shadow: 0 0 5px #999;
    -webkit-box-shadow: 0 0 5px #999;
}
/*font*/
pre span, code span {
    font-family: 'Consolas', 'Courier New', 'Microsoft JhengHei', sans-serif !important;
    font-size: 12px !important;
}
/*each line*/
li.L0, li.L1, li.L2, li.L3, li.L4, li.L5, li.L6, li.L7, li.L8, li.L9 {
    margin: 0 !important;
    padding: 2px 0 2px 4px !important;
    list-style-type:decimal !important;
    border-left: 1px solid #999;
}
/*even line*/
li.L1, li.L3, li.L5, li.L7, li.L9 {
    background-color: #f6f6f6 !important;
}
/*odd line*/
li.L0, li.L2, li.L4, li.L6, li.L8 {
    background-color: #FFF !important;
}
/*line-number background color*/
ol.linenums {
    background-color: #eee;
    margin-left: 10px;
}
</style>

<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>


接著就是在網誌編輯器測試了,用html模式輸入code

<pre class="codeblock prettyprint linenums:1">

public class HelloWorld {
    public static void main (String[] args) {
        System.out.println("Hello, world!\n");
    }
}
<pre >