本文整理汇总了C#中System.Net.Http.HttpResponseMessage.TryGetContentValue方法的典型用法代码示例。如果您正苦于以下问题:C# HttpResponseMessage.TryGetContentValue方法的具体用法?C# HttpResponseMessage.TryGetContentValue怎么用?C# HttpResponseMessage.TryGetContentValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpResponseMessage
的用法示例。
在下文中一共展示了HttpResponseMessage.TryGetContentValue方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Enrich
public HttpResponseMessage Enrich(HttpResponseMessage response)
{
PostModel post;
UrlHelper urlHelper = response.RequestMessage.GetUrlHelper();
if (response.TryGetContentValue<PostModel>(out post))
{
Enrich(post, urlHelper);
return response;
}
PostFeed feed;
if (response.TryGetContentValue<PostFeed>(out feed))
{
foreach (var p in feed.Posts)
{
Enrich(p, urlHelper);
}
string selfUrl = urlHelper.Link("DefaultApi", new { controller = "posts" });
feed.AddLink(new SelfLink(selfUrl));
}
return response;
}
示例2: BuildApiResponse
private static HttpResponseMessage BuildApiResponse(HttpRequestMessage request, HttpResponseMessage response)
{
object content;
string errorMessage = null;
if (response.TryGetContentValue(out content) && !response.IsSuccessStatusCode)
{
HttpError error = content as HttpError;
if (error != null)
{
content = null;
errorMessage = error.Message;
#if DEBUG
errorMessage = string.Concat(errorMessage, error.ExceptionMessage, error.StackTrace);
#endif
}
}
var newResponse = request.CreateResponse(response.StatusCode, new ApiResponse(response.StatusCode, content, errorMessage));
foreach (var header in response.Headers)
{
newResponse.Headers.Add(header.Key, header.Value);
}
return newResponse;
}
示例3: GetResponseMessage
private HttpResponseMessage GetResponseMessage(HttpRequestMessage request, HttpResponseMessage response)
{
object content;
if (response.TryGetContentValue(out content))
{
var error = content as HttpError;
if (content is string && !response.IsSuccessStatusCode)
{
content = new {error = content};
}
else if (error != null)
{
if (response.StatusCode == HttpStatusCode.InternalServerError)
{
error.Message = "Internal server error.";
}
content = GetErrorResult(error);
}
else
{
content = new {result = content};
}
}
if (content != null)
response.Content = new ObjectContent(content.GetType(), content, JsonFormatter);
return response;
}
示例4: Enrich
public HttpResponseMessage Enrich(HttpResponseMessage response) {
Car car;
UrlHelper urlHelper = response.RequestMessage.GetUrlHelper();
if (response.TryGetContentValue<Car>(out car)) {
Enrich(car, urlHelper);
}
IEnumerable<Car> cars;
if (response.TryGetContentValue<IEnumerable<Car>>(out cars)) {
foreach (var carItem in cars) {
Enrich(carItem, urlHelper);
}
}
return response;
}
示例5: Enrich
public HttpResponseMessage Enrich(HttpResponseMessage response)
{
TodoRepresentation todo;
var urlHelper = response.RequestMessage.GetUrlHelper();
if (response.TryGetContentValue(out todo))
{
Enrich(todo, urlHelper);
return response;
}
List<TodoRepresentation> representations;
if (response.TryGetContentValue(out representations))
{
representations.ToList().ForEach(p => Enrich(p, urlHelper));
}
return response;
}
示例6: Enrich
public HttpResponseMessage Enrich(HttpResponseMessage response) {
MediaModel media;
var urlHelper = response.RequestMessage.GetUrlHelper();
if (response.TryGetContentValue<MediaModel>(out media)) {
Enrich(media, urlHelper);
}
return response;
}
示例7: WrapResultIfNeeded
protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
{
if (!response.IsSuccessStatusCode)
{
return;
}
if (_configuration.SetNoCacheForAllResponses)
{
//Based on http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
response.Headers.CacheControl = new CacheControlHeaderValue
{
NoCache = true,
NoStore = true,
MaxAge = TimeSpan.Zero,
MustRevalidate = true
};
}
var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
?? _configuration.DefaultWrapResultAttribute;
if (!wrapAttr.WrapOnSuccess)
{
return;
}
if (IsIgnoredUrl(request.RequestUri))
{
return;
}
object resultObject;
if (!response.TryGetContentValue(out resultObject) || resultObject == null)
{
response.StatusCode = HttpStatusCode.OK;
response.Content = new ObjectContent<AjaxResponse>(
new AjaxResponse(),
_configuration.HttpConfiguration.Formatters.JsonFormatter
);
return;
}
if (resultObject is AjaxResponseBase)
{
return;
}
response.Content = new ObjectContent<AjaxResponse>(
new AjaxResponse(resultObject),
_configuration.HttpConfiguration.Formatters.JsonFormatter
);
}
示例8: SDataContent
/// <summary>
/// SDataContent transforms OData/Web API Content/Response to SData
/// </summary>
/// <param name="response">The response containing the Content/Payload to transform</param>
public SDataContent(HttpResponseMessage response)
{
origResponse = response;
originalContent = origResponse.Content;
if (originalContent == null)
{
throw new ArgumentNullException("response");
}
foreach (var header in originalContent.Headers)
{
Headers.Add(header.Key, header.Value);
}
origResponse.TryGetContentValue(out responseObject);
}
示例9: WrapResultIfNeeded
protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
{
if (!response.IsSuccessStatusCode)
{
return;
}
var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
?? DontWrapResultAttribute.Default;
if (!wrapAttr.WrapOnSuccess)
{
return;
}
object resultObject;
if (!response.TryGetContentValue(out resultObject))
{
return;
}
if (resultObject == null)
{
response.Content = new ObjectContent<AjaxResponse>(
new AjaxResponse(),
_webApiModuleConfiguration.HttpConfiguration.Formatters.JsonFormatter
);
}
if (resultObject is AjaxResponse)
{
return;
}
response.Content = new ObjectContent<AjaxResponse>(
new AjaxResponse(resultObject),
_webApiModuleConfiguration.HttpConfiguration.Formatters.JsonFormatter
);
}