本文整理汇总了C#中Microsoft.Net.Http.Headers.MediaTypeHeaderValue.?.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# MediaTypeHeaderValue.?.ToString方法的具体用法?C# MediaTypeHeaderValue.?.ToString怎么用?C# MediaTypeHeaderValue.?.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Net.Http.Headers.MediaTypeHeaderValue
的用法示例。
在下文中一共展示了MediaTypeHeaderValue.?.ToString方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FileContentResult
/// <summary>
/// Creates a new <see cref="FileContentResult"/> instance with
/// the provided <paramref name="fileContents"/> and the
/// provided <paramref name="contentType"/>.
/// </summary>
/// <param name="fileContents">The bytes that represent the file contents.</param>
/// <param name="contentType">The Content-Type header of the response.</param>
public FileContentResult(byte[] fileContents, MediaTypeHeaderValue contentType)
: base(contentType?.ToString())
{
if (fileContents == null)
{
throw new ArgumentNullException(nameof(fileContents));
}
FileContents = fileContents;
}
示例2: PhysicalFileResult
/// <summary>
/// Creates a new <see cref="PhysicalFileResult"/> instance with
/// the provided <paramref name="fileName"/> and the provided <paramref name="contentType"/>.
/// </summary>
/// <param name="fileName">The path to the file. The path must be an absolute path.</param>
/// <param name="contentType">The Content-Type header of the response.</param>
public PhysicalFileResult(string fileName, MediaTypeHeaderValue contentType)
: base(contentType?.ToString())
{
if (fileName == null)
{
throw new ArgumentNullException(nameof(fileName));
}
FileName = fileName;
}
示例3: FileStreamResult
/// <summary>
/// Creates a new <see cref="FileStreamResult"/> instance with
/// the provided <paramref name="fileStream"/> and the
/// provided <paramref name="contentType"/>.
/// </summary>
/// <param name="fileStream">The stream with the file.</param>
/// <param name="contentType">The Content-Type header of the response.</param>
public FileStreamResult(Stream fileStream, MediaTypeHeaderValue contentType)
: base(contentType?.ToString())
{
if (fileStream == null)
{
throw new ArgumentNullException(nameof(fileStream));
}
FileStream = fileStream;
}
示例4: ViewComponentResult_SetsContentTypeHeader
public async Task ViewComponentResult_SetsContentTypeHeader(
MediaTypeHeaderValue contentType,
string expectedContentTypeHeaderValue)
{
// Arrange
var descriptor = new ViewComponentDescriptor()
{
FullName = "Full.Name.Text",
ShortName = "Text",
Type = typeof(TextViewComponent),
MethodInfo = typeof(TextViewComponent).GetMethod(nameof(TextViewComponent.Invoke)),
};
var actionContext = CreateActionContext(descriptor);
var contentTypeBeforeViewResultExecution = contentType?.ToString();
var viewComponentResult = new ViewComponentResult()
{
Arguments = new { name = "World!" },
ViewComponentName = "Text",
ContentType = contentType,
TempData = _tempDataDictionary,
};
// Act
await viewComponentResult.ExecuteResultAsync(actionContext);
// Assert
Assert.Equal(expectedContentTypeHeaderValue, actionContext.HttpContext.Response.ContentType);
// Check if the original instance provided by the user has not changed.
// Since we do not have access to the new instance created within the view executor,
// check if at least the content is the same.
var contentTypeAfterViewResultExecution = contentType?.ToString();
Assert.Equal(contentTypeBeforeViewResultExecution, contentTypeAfterViewResultExecution);
}
示例5: ContentResult_ExecuteResultAsync_SetContentTypeAndEncoding_OnResponse
public async Task ContentResult_ExecuteResultAsync_SetContentTypeAndEncoding_OnResponse(
MediaTypeHeaderValue contentType,
string content,
string responseContentType,
string expectedContentType,
byte[] expectedContentData)
{
// Arrange
var contentResult = new ContentResult
{
Content = content,
ContentType = contentType?.ToString()
};
var httpContext = GetHttpContext();
var memoryStream = new MemoryStream();
httpContext.Response.Body = memoryStream;
httpContext.Response.ContentType = responseContentType;
var actionContext = GetActionContext(httpContext);
// Act
await contentResult.ExecuteResultAsync(actionContext);
// Assert
var finalResponseContentType = httpContext.Response.ContentType;
Assert.Equal(expectedContentType, finalResponseContentType);
Assert.Equal(expectedContentData, memoryStream.ToArray());
}
示例6: ViewResult_SetsContentTypeHeader
public async Task ViewResult_SetsContentTypeHeader(
MediaTypeHeaderValue contentType,
string expectedContentTypeHeaderValue)
{
// Arrange
var viewName = "myview";
var httpContext = GetHttpContext();
var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
var viewEngine = new Mock<IViewEngine>();
var view = Mock.Of<IView>();
var contentTypeBeforeViewResultExecution = contentType?.ToString();
viewEngine.Setup(e => e.FindView(context, "myview"))
.Returns(ViewEngineResult.Found("myview", view));
var viewResult = new ViewResult
{
ViewName = viewName,
ViewEngine = viewEngine.Object,
ContentType = contentType
};
// Act
await viewResult.ExecuteResultAsync(context);
// Assert
Assert.Equal(expectedContentTypeHeaderValue, httpContext.Response.ContentType);
// Check if the original instance provided by the user has not changed.
// Since we do not have access to the new instance created within the view executor,
// check if at least the content is the same.
var contentTypeAfterViewResultExecution = contentType?.ToString();
Assert.Equal(contentTypeBeforeViewResultExecution, contentTypeAfterViewResultExecution);
}
示例7: ExecuteAsync
/// <summary>
/// Asynchronously renders the specified <paramref name="view"/> to the response body.
/// </summary>
/// <param name="view">The <see cref="IView"/> to render.</param>
/// <param name="actionContext">The <see cref="ActionContext"/> for the current executing action.</param>
/// <param name="viewData">The <see cref="ViewDataDictionary"/> for the view being rendered.</param>
/// <param name="tempData">The <see cref="ITempDataDictionary"/> for the view being rendered.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous rendering.</returns>
public static async Task ExecuteAsync(
IView view,
ActionContext actionContext,
ViewDataDictionary viewData,
ITempDataDictionary tempData,
HtmlHelperOptions htmlHelperOptions,
MediaTypeHeaderValue contentType)
{
if (view == null)
{
throw new ArgumentNullException(nameof(view));
}
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
if (viewData == null)
{
throw new ArgumentNullException(nameof(viewData));
}
if (tempData == null)
{
throw new ArgumentNullException(nameof(tempData));
}
if (htmlHelperOptions == null)
{
throw new ArgumentNullException(nameof(htmlHelperOptions));
}
var response = actionContext.HttpContext.Response;
if (contentType != null && contentType.Encoding == null)
{
// Do not modify the user supplied content type, so copy it instead
contentType = contentType.Copy();
contentType.Encoding = Encoding.UTF8;
}
// Priority list for setting content-type:
// 1. passed in contentType (likely set by the user on the result)
// 2. response.ContentType (likely set by the user in controller code)
// 3. ViewExecutor.DefaultContentType (sensible default)
response.ContentType = contentType?.ToString() ?? response.ContentType ?? DefaultContentType.ToString();
using (var writer = new HttpResponseStreamWriter(response.Body, contentType?.Encoding ?? DefaultContentType.Encoding))
{
var viewContext = new ViewContext(
actionContext,
view,
viewData,
tempData,
writer,
htmlHelperOptions);
await view.RenderAsync(viewContext);
// Invoke FlushAsync to ensure any buffered content is asynchronously written to the underlying
// response. In the absence of this line, the buffer gets synchronously written to the response
// as part of the Dispose which has a perf impact.
await writer.FlushAsync();
}
}
示例8: ExecuteAsync
/// <summary>
/// Executes a view asynchronously.
/// </summary>
/// <param name="actionContext">The <see cref="ActionContext"/> associated with the current request.</param>
/// <param name="view">The <see cref="IView"/>.</param>
/// <param name="viewData">The <see cref="ViewDataDictionary"/>.</param>
/// <param name="tempData">The <see cref="ITempDataDictionary"/>.</param>
/// <param name="contentType">
/// The content-type header value to set in the response. If <c>null</c>, <see cref="DefaultContentType"/> will be used.
/// </param>
/// <param name="statusCode">
/// The HTTP status code to set in the response. May be <c>null</c>.
/// </param>
/// <returns>A <see cref="Task"/> which will complete when view execution is completed.</returns>
public virtual async Task ExecuteAsync(
ActionContext actionContext,
IView view,
ViewDataDictionary viewData,
ITempDataDictionary tempData,
MediaTypeHeaderValue contentType,
int? statusCode)
{
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
if (view == null)
{
throw new ArgumentNullException(nameof(view));
}
var services = actionContext.HttpContext.RequestServices;
if (viewData == null)
{
var metadataProvider = services.GetRequiredService<IModelMetadataProvider>();
viewData = new ViewDataDictionary(metadataProvider);
}
if (tempData == null)
{
tempData = services.GetRequiredService<ITempDataDictionary>();
}
var response = actionContext.HttpContext.Response;
if (contentType != null && contentType.Encoding == null)
{
// Do not modify the user supplied content type, so copy it instead
contentType = contentType.Copy();
contentType.Encoding = Encoding.UTF8;
}
// Priority list for setting content-type:
// 1. passed in contentType (likely set by the user on the result)
// 2. response.ContentType (likely set by the user in controller code)
// 3. ViewExecutor.DefaultContentType (sensible default)
response.ContentType = contentType?.ToString() ?? response.ContentType ?? DefaultContentType.ToString();
if (statusCode != null)
{
response.StatusCode = statusCode.Value;
}
var encoding = contentType?.Encoding ?? DefaultContentType.Encoding;
using (var writer = WriterFactory.CreateWriter(response.Body, encoding))
{
var viewContext = new ViewContext(
actionContext,
view,
viewData,
tempData,
writer,
ViewOptions.HtmlHelperOptions);
DiagnosticSource.BeforeView(view, viewContext);
await view.RenderAsync(viewContext);
DiagnosticSource.AfterView(view, viewContext);
// Perf: Invoke FlushAsync to ensure any buffered content is asynchronously written to the underlying
// response asynchronously. In the absence of this line, the buffer gets synchronously written to the
// response as part of the Dispose which has a perf impact.
await writer.FlushAsync();
}
}
示例9: ExecuteAsync_SetsContentTypeAndEncoding
public async Task ExecuteAsync_SetsContentTypeAndEncoding(
MediaTypeHeaderValue contentType,
string responseContentType,
string expectedContentType)
{
// Arrange
var view = CreateView(async (v) =>
{
await v.Writer.WriteAsync("abcd");
});
var context = new DefaultHttpContext();
var memoryStream = new MemoryStream();
context.Response.Body = memoryStream;
context.Response.ContentType = responseContentType;
var actionContext = new ActionContext(
context,
new RouteData(),
new ActionDescriptor());
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
var viewExecutor = CreateViewExecutor();
// Act
await viewExecutor.ExecuteAsync(
actionContext,
view,
viewData,
Mock.Of<ITempDataDictionary>(),
contentType?.ToString(),
statusCode: null);
// Assert
MediaTypeAssert.Equal(expectedContentType, context.Response.ContentType);
Assert.Equal("abcd", Encoding.UTF8.GetString(memoryStream.ToArray()));
}