我创建了一个Web API来从SQL Server检索数据。 我使用OData(v4)作为控制器。
这是主控制器的条纹版本。
public class ProtocolsController : ODataController
{
private readonly MyDbContext _db = new MyDbContext();
protected override void Dispose(bool disposing)
{
_db.Dispose();
base.Dispose(disposing);
}
//Returns all records
[EnableQuery]
public IQueryable<Protocol> GetProtocols()
{
return _db.Protocols;
}
//Returns a single record
[ODataRoute("Protocols(PYear={year}, PSerialNo={serial}, PType={type})")]
[EnableQuery]
public IHttpActionResult GetProtocol([FromODataUri] int year, [FromODataUri] int serial, [FromODataUri] int type)
{
return Ok(_db.Protocols.Where(p => p.PYear == year && p.PSerialNo == serial && p.PType == type));
}
}
这是我发出的GET请求
http://localhost:49959/Protocols(PYear=2015, PSerialNo=16, PType =1)
这是该请求返回的JSON
{
"@odata.context": "http://192.168.2.26:49959/$metadata#Protocols",
"value": [
{
"PYear": 2015,
"PSerialNo": 16,
"PType": 1,
"PProtoTime": null,
"PNumClass": 0,
"PIncomingDocSn": "1234",
"PIncomingYear": 2015,
"PIncomingDate": "2015-01-02T00:00:00+02:00",
"PSendMethod": 5,
"PCreatorDepartment": 3,
"PCreator": 1,
"POwnerDepartment": 3,
"POwner": 1,
"PDocumentDate": "2015-01-02T00:00:00+02:00",
"PProtoDate": "2015-01-02T00:00:00+02:00",
"PDocClass": 3,
"PPlace": 5,
"PAuthority": 3,
"PGrading": 1,
"PDescription": "ΧΟΡ. ΑΠΟΦΑΣΗΣ ΤΗΣ ΑΡΜΟΔ. ΕΠΙΤΡΟΠΗΣ ΓΙΑ ΤΗΝ ΕΚΔΟΣΗ ΒΙΒΛΙΑΡΙΟΥ ΠΕΡΙΘ. ΑΝΑΣΦ/ΣΤΩΝ ΚΑΙ ΟΙΚΟΝΟΜΙΚΑ ΑΔΥΝΑΤΩΝ",
"PComments": "",
"PCancelled": 0,
"PAddress": "Address",
"PTown": "Area",
"PZip": "TK",
"PEmail": "Email",
"PFax": "FAX",
"PTel": "Phone",
"PCab": "(Φ.Γ) ΓΡΑΦΕΙΟ ΚΟΙΝΩΝΙΚΗΣ ΜΕΡΙΜΝΑΣ",
"PMoreDestinations": null,
"PUdfC1": "txt1",
"PUdfC2": "txt2",
"PUdfC3": "txt3",
"PUdfD1": "2015-12-23T00:00:00+02:00",
"PUdfD2": "2015-12-23T00:00:00+02:00",
"PUdfD3": "2015-12-23T00:00:00+02:00",
"PUdfN1": 1,
"PUdfN2": 2,
"PUdfN3": 3,
"PTheme": 1,
"PSummary": "{\\rtf1\\ansi\\ansicpg1253\\deff0\\deflang1032{\\fonttbl{\\f0\\fswiss\\fcharset0 Tahoma;}{\\f1\\fnil\\fcharset161 MS Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\lang1033\\f0\\fs16 summary\\lang1032\\f1 \r\n\\par }\r\n",
"PName": "ΠΑΡΑΣΥΡΗΣ ΓΕΩΡΓΙΟΣ ΤΟΥ ΙΩΑΝΝΗ",
"PDocState": 2,
"PCompletionDate": "2015-12-23T13:39:51+02:00",
"PCompletionNumb": 1
}
]
}
正如你所看到的,我的数据中有非拉丁字符(希腊语btw)。当我向Postman或Fiddler提出请求时,我得到了上述结果。
当我从任何浏览器尝试相同的请求时,我得到了这个
{
"@odata.context":"http://192.168.2.26:49959/$metadata#Protocols","value":[
{
"PYear":2015,"PSerialNo":16.0000,"PType":1,"PProtoTime":null,"PNumClass":0,"PIncomingDocSn":"1234","PIncomingYear":2015,"PIncomingDate":"2015-01-02T00:00:00+02:00","PSendMethod":5,"PCreatorDepartment":3,"PCreator":1,"POwnerDepartment":3,"POwner":1,"PDocumentDate":"2015-01-02T00:00:00+02:00","PProtoDate":"2015-01-02T00:00:00+02:00","PDocClass":3,"PPlace":5,"PAuthority":3,"PGrading":1,"PDescription":"\u03a7\u039f\u03a1. \u0391\u03a0\u039f\u03a6\u0391\u03a3\u0397\u03a3 \u03a4\u0397\u03a3 \u0391\u03a1\u039c\u039f\u0394. \u0395\u03a0\u0399\u03a4\u03a1\u039f\u03a0\u0397\u03a3 \u0393\u0399\u0391 \u03a4\u0397\u039d \u0395\u039a\u0394\u039f\u03a3\u0397 \u0392\u0399\u0392\u039b\u0399\u0391\u03a1\u0399\u039f\u03a5 \u03a0\u0395\u03a1\u0399\u0398. \u0391\u039d\u0391\u03a3\u03a6/\u03a3\u03a4\u03a9\u039d \u039a\u0391\u0399 \u039f\u0399\u039a\u039f\u039d\u039f\u039c\u0399\u039a\u0391 \u0391\u0394\u03a5\u039d\u0391\u03a4\u03a9\u039d","PComments":"","PCancelled":0,"PAddress":"Address","PTown":"Area","PZip":"TK","PEmail":"Email","PFax":"FAX","PTel":"Phone","PCab":"(\u03a6.\u0393) \u0393\u03a1\u0391\u03a6\u0395\u0399\u039f \u039a\u039f\u0399\u039d\u03a9\u039d\u0399\u039a\u0397\u03a3 \u039c\u0395\u03a1\u0399\u039c\u039d\u0391\u03a3","PMoreDestinations":null,"PUdfC1":"txt1","PUdfC2":"txt2","PUdfC3":"txt3","PUdfD1":"2015-12-23T00:00:00+02:00","PUdfD2":"2015-12-23T00:00:00+02:00","PUdfD3":"2015-12-23T00:00:00+02:00","PUdfN1":1.0000,"PUdfN2":2.0000,"PUdfN3":3.0000,"PTheme":1,"PSummary":"{\\rtf1\\ansi\\ansicpg1253\\deff0\\deflang1032{\\fonttbl{\\f0\\fswiss\\fcharset0 Tahoma;}{\\f1\\fnil\\fcharset161 MS Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\lang1033\\f0\\fs16 summary\\lang1032\\f1 \r\n\\par }\r\n","PName":"\u03a0\u0391\u03a1\u0391\u03a3\u03a5\u03a1\u0397\u03a3 \u0393\u0395\u03a9\u03a1\u0393\u0399\u039f\u03a3 \u03a4\u039f\u03a5 \u0399\u03a9\u0391\u039d\u039d\u0397","PDocState":2,"PCompletionDate":"2015-12-23T13:39:51+02:00","PCompletionNumb":1
}
]
}
由于某种原因,非拉丁字符被返回URL编码。 我需要在API端设置一些东西或者我必须使用浏览器吗?
P.S。我不知道它是否具有任何重要性,但我也使用Owin。
答案 0 :(得分:2)
使用\uXXXX
形式的字符串数据的响应只是使用与包含希腊字符的响应不同的character encoding(不是URL编码)。字符串在功能上是等价的。
您的浏览器可能会发送Accept-Charset
标头,其值为us-ascii
;或者它可能在charset
标题中发送Accept
参数(不太可能)。另一方面,Postman和Fiddler可能根本不发送Accept-Charset
(因为它们只发送你明确告诉他们的内容),因此服务器上的字符编码默认为utf-8
(可以直接代表希腊语)。使用浏览器中的开发人员工具检查随请求发送的标头。