1

我创建了一个 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
    }
  ]
}

如您所见,我的数据中有非拉丁字符(希腊语)。当我从 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 端设置什么或者我必须与我的浏览器做些什么吗?

PS我不知道它是否重要,但我也使用Owin。

4

1 回答 1

2

带有表单字符串数据的响应\uXXXX只是使用与包含希腊字符的响应不同的字符编码(不是 URL 编码)。字符串在功能上是等效的。

您的浏览器可能正在发送Accept-Charset一个值为us-ascii;的标头。或者它可能charset在标头中发送一个参数Accept(不太可能)。另一方面,Postman 和 Fiddler 可能根本不发送Accept-Charset(因为它们只发送您明确告诉他们的内容),因此服务器上的字符编码默认为utf-8(可以直接表示希腊语)。使用浏览器中的开发人员工具检查与您的请求一起发送的标头。

于 2016-04-28T16:56:57.673 回答