2021年4月25日 星期日

[已上文]LinqPad 的使用

設定連線



LINQPad有三種編寫方式

一、Expression: 單行程式或是單個LINQ陳述式

範例1、

DateTime.Now.ToString("yyyy/MM/dd")


範例2、

以北風資料庫為例

from x in Products

select x.ProductName


二、Statement(s): 多行的程式片段

var w = from x in Products

select x.ProductName;

w.Dump();


三、Program: 需要撰寫Class或是Method時

void Main()
{
    Test t = new Test();
    t.Hello().Dump();
    World().Dump();
}

string World(){
    return "World";
}

class Test{
    public string Hello(){
        return "Hello";
    }
}


參考資料:

LINQPad: 每個.NET工程師都要有的一隻箭


2021年4月2日 星期五

[已上文]web api 吐出 json 範例

 以下例出基本的 web api 吐出 json 範例


一、範例一

using Newtonsoft.Json.Linq;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebApplication1.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class personalTestController : ApiController
    {
        [HttpPost]
        public object CreateEvent()
        {
            JArray newJa = new JArray();
            JObject tmpJo01 = new JObject();
            tmpJo01.Add(new JProperty("code_stat", "Y"));
            newJa.Add(tmpJo01);

            return new
            {
                Result = "F",
                Message = "Message",
                TotalRec = "100",
                Data = newJa
            };
        }
    }
}


二、範例二

using Newtonsoft.Json.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApplication2.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public HttpResponseMessage Get()
        {
            JObject result = new JObject();
            result.Add(new JProperty("UserName", "李先生"));



            JArray jar1 = new JArray();
            JObject jobTemp = new JObject();

            jobTemp.Add(new JProperty("GroupId", "1"));
            jobTemp.Add(new JProperty("GroupName", "群組一"));
            jar1.Add(jobTemp);

            jobTemp = new JObject();
            jobTemp.Add(new JProperty("GroupId", "2"));
            jobTemp.Add(new JProperty("GroupName", "群組二"));
            jar1.Add(jobTemp);

            result.Add(new JProperty("UserGroup", jar1));

            return new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(result), System.Text.Encoding.UTF8, "application/json")
            };
        }

    }
}



[已上文]web api xml 範例 HttpResponseMessage、HttpStatusCode 的使用

以下範例為,藉由 web api 吐出 xml 格式來示範 HttpResponseMessage、HttpStatusCode 的用法。

using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebApplication1.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class personalTestController : ApiController
    {
        [HttpPost]
        public HttpResponseMessage CreateEvent()
        {
            string result = "";
            result += "<?xmlversion=\"1.0\"encoding=\"utf-8\"?>";
            result += "<bookstore>";
            result += "  <book>";
            result += "    <title>Everyday Italian</title>";
            result += "    <author>Giada De Laurentiis</author>";
            result += "    <year>2005</year>";
            result += "  </book>";
            result += "</bookstore>";

            return new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content = new StringContent(result, System.Text.Encoding.UTF8, "application/xml")
            };
        }
    }
}


參考資料:

[Web API] 使用 HttpResponseMessage 與 HttpResponseException 的差異