2018年12月24日 星期一

[C#][Json]讀Json處理小數點

讀JSON檔的時候,有些有小數的欄位太長
想去掉小數點,可以在Entity上面加converter
透過converter,取到的資料已經做好處理
參考範例如下

public class TestEntity {
    [JsonConverter(typeof(CustomDecimalNullConverter))]
    public decimal val {get; set;}
}


void Main() {   
    var settings = new JsonSerializerSettings();
    settings.Converters.Add(new CustomDecimalNullConverter());
    var result = JsonConvert.DeserializeObject>(json, settings);
}

// For completeness: A stupid example converter
class CustomDecimalNullConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(decimal);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
        {
            return 0m;
        }
        else
        {
            return Convert.ToDecimal(reader.Value);
        }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue((decimal)value);
    }
}

Ref: https://stackoverflow.com/questions/51885268/custom-rule-for-deserializing-decimal-values-in-json-net
 

2018年12月11日 星期二

[IIS]HTTP 錯誤 500.19 - Internal Server Error










最近公司因應稽核,把權限更改為更嚴謹
原本測試區的server也變成權限不足無法連

 



網路上找到的解法是,在資料夾的安全性裡
新增Authenticated Users,就可以連了













不過最後有提到,要設定IIS AppPool就沒去研究了
因為不是新增應用程式,而且測試環境沒那麼嚴謹

Ref:
https://dotblogs.com.tw/caubekimo/2010/09/19/17805

2017年8月25日 星期五

[.Net] Connect Exchange Server


為了要可以使用Exchage server寄信,必須從NuGet下載Exchange Web Services (EWS)
後來測試,怎麼連不都連不上,才發現原來有相依.Net的版本(好像要3.5以上)

另外一個要注意的是網址,用的是exchange.asmx,不是原本的xxx.com/owa/auth/logon.aspx
這個環節也試了很久,後來看到別人寫的範例都是.asmx,想說試看看就可以連了




Sample Code:

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);//版本預設值最新版
service.Credentials = new WebCredentials(ConfigurationManager.AppSettings["mailAccount"], ConfigurationManager.AppSettings["mailPassword"]);
service.Url = new Uri("https://xxxx.com/ews/exchange.asmx"); // Server路徑

EmailMessage email = new EmailMessage(service);
  
email.ToRecipients.Add(contact.Name, contact.Email); //收件者

email.CcRecipients.Add("AAA", "test1@mail.com"); //CC
email.BccRecipients.Add("BBB", "test2@mail.com "); //密件副本
email.Subject = mailJob.Subject; //主旨
email.Body = mailJob.Content; //內容
email.Body.BodyType = BodyType.HTML; //格式
email.Send();
Ref:
http://blog.oscarscode.com/dot-net/get-started-with-exchange-web-services-ews/
https://stackoverflow.com/questions/13517323/exchange-web-service-api-and-401-unauthorized-exception
https://blog.miniasp.com/post/2007/11/01/The-remote-certificate-is-invalid-according-to-the-validation-procedure.aspx

2016年10月14日 星期五

[SQL]Remove duplicates

This will also take care of duplicates (return one row for each user_id):


SELECT * FROM (
  SELECT u.*, FIRST_VALUE(u.rowid) OVER(PARTITION BY u.user_id ORDER BY u.date DESC) AS last_rowid
  FROM users u
) u2
WHERE u2.rowid = u2.last_rowid




Ref:
http://stackoverflow.com/questions/121387/fetch-the-row-which-has-the-max-value-for-a-column

2016年7月15日 星期五

[SQL SERVER] convert comma Separated String into rows




DECLARE @str VARCHAR(4000)= '6,7,7,8,10,12,13,14,16,44,46,47,394,396,417,488,714,717,718,719,722,725,811,818,832,833,836,837,846,913,914,919,922,923,924,925,926,927,927,928,929,929,930,931,932,934,935,1029,1072,1187,1188,1192,1196,1197,1199,1199,1199,1199,1200,1201,1202,1203,1204,1205,1206,1207,1208,1209,1366,1367,1387,1388,1666,1759,1870,2042,2045,2163,2261,2374,2445,2550,2676,2879,2880,2881,2892,2893,2894';

SELECT t.c.value('.', 'VARCHAR(1000)')
FROM (
  SELECT x = CAST('' +
   REPLACE(@str , ',', '') + '' AS XML)
 ) a
CROSS APPLY x.nodes('/t') t(c);





Ref:

2016年6月21日 星期二

String.IsNullOrEmpty in JavaScript

Starting with:
return (!value || value == undefined || value == "" || value.length == 0);
Looking at the last condition, if value == "", it's length MUST be 0. Therefore drop it:
return (!value || value == undefined || value == "");
But wait! In JS, an empty string is false. Therefore, drop value == "":
return (!value || value == undefined);
And !undefined is true, so that check isn't needed. So we have:
return (!value);
And we don't need parentheses:
return !value
Q.E.D.


http://codereview.stackexchange.com/questions/5572/string-isnullorempty-in-javascript

2016年6月2日 星期四

[jQuery]add dynamic rows



Ref:
http://stackoverflow.com/questions/2145012/adding-rows-dynamically-with-jquery

http://ssiddique.info/different-ways-to-add-row-to-a-table-using-jquery.html

http://stackoverflow.com/questions/16183231/jquery-append-and-remove-dynamic-table-row