Ⅰ webapi讀取json webapi讀取json時,獲取的數據為空(webhook)
轉載 在使用Web Api的時候,有時候只想返回JSON;實現這一功能有多種方法,本文提供兩種方式,一種傳統的,一種作者認為是正確的方法。
JSON in Web API – the formatter based approach
只支持JSON最普遍的做法是:首先清除其他所有的formatters,然後只保留JsonMediaTypeFormatter。
有了HttpConfiguration的實例,你將會很簡單的清除所有formatters,然後重新添加JsonMediaTypeFormatter。
實現代碼如下:
configuration.Formatters.Clear();
configuration.Formatters.Add(new JsonMediaTypeFormatter());這種方式雖然可以實現功能,但是所有的conent negotiation還是會發生,這就會產生以下額外的開銷了。因為,你已經知道要返回的結果了,也只想返回Json,其他的content negotiation都不需要了。
下面的方法可以很好的解決這個問題。JSON in Web API – the conneg based approach
最好的方法是使用自定義的只返回Json Result的content negotiation代替Web Api中默認的content negotiation。
Conneg通過實現IContentNegotiator的Negotiator方法實現擴展。Negotiator方法返回ContentNegotiationResult(它包裝了你選擇的headers和formatter)。
下面的方法通過傳遞一個JsonMediaTypeFormatter給自定義的conneg negotiator,讓它一直返回applicaton/json 的content-type以及JsonMediaTypeFormatter。這種方法避免了每次請求都要重新創建一次formatter。
代碼如下:
public class JsonContentNegotiator : IContentNegotiator
{
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
return result;
}
}接下來,你需要在HttpConfiguration實例上注冊你的新的實現機制:var jsonFormatter = new JsonMediaTypeFormatter();
//optional: set serializer settings here
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
通過替換默認的DefaultContentNegotiator,我們使用我們自定義的JsonContentNegotiator,它只支持Json,而且可以馬上返回。
如果你想更深入的了解Content Negotiation的知識,你可以查看作者的這篇文章。
總結
通過使用自定義的JsonContentNegotiator替換系統默認的DefaultContentNegotiator,很好的實現Web Api只返回Json的功能,而且沒有額外的開銷。
Ⅱ 在WebApi中返回一個JSON格式的數據,如何到客戶端就變了
轉載 在使用Web Api的時候,有時候只想返回JSON;實現這一功能有多種方法,本文提供兩種方式,一種傳統的,一種作者認為是正確的方法。
JSON in Web API – the formatter based approach
只支持JSON最普遍的做法是:首先清除其他所有的formatters,然後只保留JsonMediaTypeFormatter。
有了HttpConfiguration的實例,你將會很簡單的清除所有formatters,然後重新添加JsonMediaTypeFormatter。
實現代碼如下:
configuration.Formatters.Clear();
configuration.Formatters.Add(new JsonMediaTypeFormatter());這種方式雖然可以實現功能,但是所有的conent negotiation還是會發生,這就會產生以下額外的開銷了。因為,你已經知道要返回的結果了,也只想返回Json,其他的content negotiation都不需要了。
下面的方法可以很好的解決這個問題。
JSON in Web API – the conneg based approach
最好的方法是使用自定義的只返回Json Result的content negotiation代替Web Api中默認的content negotiation。
Conneg通過實現IContentNegotiator的Negotiator方法實現擴展。Negotiator方法返回ContentNegotiationResult(它包裝了你選擇的headers和formatter)。
下面的方法通過傳遞一個JsonMediaTypeFormatter給自定義的conneg negotiator,讓它一直返回applicaton/json 的content-type以及JsonMediaTypeFormatter。這種方法避免了每次請求都要重新創建一次formatter。
代碼如下:
public class JsonContentNegotiator : IContentNegotiator
{
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
return result;
}
}接下來,你需要在HttpConfiguration實例上注冊你的新的實現機制:
var jsonFormatter = new JsonMediaTypeFormatter();
//optional: set serializer settings here
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
通過替換默認的DefaultContentNegotiator,我們使用我們自定義的JsonContentNegotiator,它只支持Json,而且可以馬上返回。
如果你想更深入的了解Content Negotiation的知識,你可以查看作者的這篇文章。
總結
通過使用自定義的JsonContentNegotiator替換系統默認的DefaultContentNegotiator,很好的實現Web Api只返回Json的功能,而且沒有額外的開銷。
Ⅲ C#如何接受api傳來的json數據
不知道你使用的是哪種api,如果是webapi的話可以用HttpClient 訪問 api 獲取json數據。
Ⅳ webapi返回json怎麼接值
返回的時候封裝成json即可。參考:Studentst1=newStudent(1,"dg",18,newDate());Studentst2=newStudent(2,"dg",18,newDate());Studentst3=newStudent(3,"dg",18,newDate());Studentst4=newStudent(4,"dg",18