本文整理汇总了C#中ObjectResult.OnFormatting方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectResult.OnFormatting方法的具体用法?C# ObjectResult.OnFormatting怎么用?C# ObjectResult.OnFormatting使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectResult
的用法示例。
在下文中一共展示了ObjectResult.OnFormatting方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteAsync
/// <summary>
/// Executes the <see cref="ObjectResult"/>.
/// </summary>
/// <param name="context">The <see cref="ActionContext"/> for the current request.</param>
/// <param name="result">The <see cref="ObjectResult"/>.</param>
/// <returns>
/// A <see cref="Task"/> which will complete once the <see cref="ObjectResult"/> is written to the response.
/// </returns>
public virtual Task ExecuteAsync(ActionContext context, ObjectResult result)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
ValidateContentTypes(result.ContentTypes);
var formatters = result.Formatters;
if (formatters == null || formatters.Count == 0)
{
formatters = GetDefaultFormatters();
}
var objectType = result.DeclaredType;
if (objectType == null || objectType == typeof(object))
{
objectType = result.Value?.GetType();
};
var formatterContext = new OutputFormatterWriteContext(
context.HttpContext,
WriterFactory,
objectType,
result.Value);
var selectedFormatter = SelectFormatter(formatterContext, result.ContentTypes, formatters);
if (selectedFormatter == null)
{
// No formatter supports this.
Logger.LogWarning("No output formatter was found to write the response.");
context.HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable;
return TaskCache.CompletedTask;
}
Logger.LogVerbose(
"Selected output formatter '{OutputFormatter}' and content type " +
"'{ContentType}' to write the response.",
selectedFormatter.GetType().FullName,
formatterContext.ContentType);
Logger.ObjectResultExecuting(context);
result.OnFormatting(context);
return selectedFormatter.WriteAsync(formatterContext);
}
示例2: ExecuteAsync
/// <summary>
/// Executes the <see cref="ObjectResult"/>.
/// </summary>
/// <param name="context">The <see cref="ActionContext"/> for the current request.</param>
/// <param name="result">The <see cref="ObjectResult"/>.</param>
/// <returns>
/// A <see cref="Task"/> which will complete once the <see cref="ObjectResult"/> is written to the response.
/// </returns>
public virtual Task ExecuteAsync(ActionContext context, ObjectResult result)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
// If the user sets the content type both on the ObjectResult (example: by Produces) and Response object,
// then the one set on ObjectResult takes precedence over the Response object
if (result.ContentTypes == null || result.ContentTypes.Count == 0)
{
var responseContentType = context.HttpContext.Response.ContentType;
if (!string.IsNullOrEmpty(responseContentType))
{
if (result.ContentTypes == null)
{
result.ContentTypes = new MediaTypeCollection();
}
result.ContentTypes.Add(responseContentType);
}
}
ValidateContentTypes(result.ContentTypes);
var formatters = result.Formatters;
if (formatters == null || formatters.Count == 0)
{
formatters = OptionsFormatters;
}
var objectType = result.DeclaredType;
if (objectType == null || objectType == typeof(object))
{
objectType = result.Value?.GetType();
}
var formatterContext = new OutputFormatterWriteContext(
context.HttpContext,
WriterFactory,
objectType,
result.Value);
var selectedFormatter = SelectFormatter(formatterContext, result.ContentTypes, formatters);
if (selectedFormatter == null)
{
// No formatter supports this.
Logger.NoFormatter(formatterContext);
context.HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable;
return TaskCache.CompletedTask;
}
Logger.FormatterSelected(selectedFormatter, formatterContext);
Logger.ObjectResultExecuting(context);
result.OnFormatting(context);
return selectedFormatter.WriteAsync(formatterContext);
}