在.NET v5 (之前称为.NET Core v5)中,这变得更加容易实现。在Microsoft.AspNetCore.Mvc.Core DLL中可以找到ControllerBase类,其中包含许多返回从IActionResult继承的对象的方法。 IActionResult是HttpResponseMessage的替代品。以下是我移植的一个基于.NET Framework v4.6的Web API方法,它用于返回文件。
[HttpGet]
public IHttpActionResult GetFooBar()
{
var fileContentByteArray = foodBarDomainService.GetExeByteArray();
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(fileContentByteArray)
};
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "FooBar.exe"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return ResponseMessage(result);
}
这是我将其移植到.NET v5的代码:
[HttpGet("GetFooBar", Name = "GetFooBar")]
public IActionResult GetFooBar(string token)
{
var fileContentByteArray = foodBarDomainService.GetExeByteArray();
return File(fileContentByteArray, "application/octet-stream", "FooBar.exe");
}
以下是返回实现了
IActionResult接口的类以服务于特定目的的所有方法的完整列表:
public virtual AcceptedResult Accepted(Uri uri, [ActionResultObjectValue] object value);
public virtual AcceptedResult Accepted(string uri, [ActionResultObjectValue] object value);
public virtual AcceptedResult Accepted(Uri uri);
public virtual AcceptedResult Accepted([ActionResultObjectValue] object value);
public virtual AcceptedResult Accepted();
public virtual AcceptedResult Accepted(string uri);
public virtual AcceptedAtActionResult AcceptedAtAction(string actionName, string controllerName, object routeValues, [ActionResultObjectValue] object value);
public virtual AcceptedAtActionResult AcceptedAtAction(string actionName, object routeValues, [ActionResultObjectValue] object value);
public virtual AcceptedAtActionResult AcceptedAtAction(string actionName);
public virtual AcceptedAtActionResult AcceptedAtAction(string actionName, string controllerName);
public virtual AcceptedAtActionResult AcceptedAtAction(string actionName, [ActionResultObjectValue] object value);
public virtual AcceptedAtActionResult AcceptedAtAction(string actionName, string controllerName, [ActionResultObjectValue] object routeValues);
public virtual AcceptedAtRouteResult AcceptedAtRoute(string routeName);
public virtual AcceptedAtRouteResult AcceptedAtRoute(object routeValues, [ActionResultObjectValue] object value);
public virtual AcceptedAtRouteResult AcceptedAtRoute(string routeName, object routeValues, [ActionResultObjectValue] object value);
public virtual AcceptedAtRouteResult AcceptedAtRoute(string routeName, object routeValues);
public virtual AcceptedAtRouteResult AcceptedAtRoute([ActionResultObjectValue] object routeValues);
public virtual BadRequestResult BadRequest();
public virtual BadRequestObjectResult BadRequest([ActionResultObjectValue] object error);
public virtual BadRequestObjectResult BadRequest([ActionResultObjectValue] ModelStateDictionary modelState);
public virtual ChallengeResult Challenge(params string[] authenticationSchemes);
public virtual ChallengeResult Challenge();
public virtual ChallengeResult Challenge(AuthenticationProperties properties);
public virtual ChallengeResult Challenge(AuthenticationProperties properties, params string[] authenticationSchemes);
public virtual ConflictResult Conflict();
public virtual ConflictObjectResult Conflict([ActionResultObjectValue] object error);
public virtual ConflictObjectResult Conflict([ActionResultObjectValue] ModelStateDictionary modelState);
public virtual ContentResult Content(string content);
public virtual ContentResult Content(string content, MediaTypeHeaderValue contentType);
public virtual ContentResult Content(string content, string contentType, Encoding contentEncoding);
public virtual ContentResult Content(string content, string contentType);
public virtual CreatedResult Created(Uri uri, [ActionResultObjectValue] object value);
public virtual CreatedResult Created(string uri, [ActionResultObjectValue] object value);
public virtual CreatedAtActionResult CreatedAtAction(string actionName, string controllerName, object routeValues, [ActionResultObjectValue] object value);
public virtual CreatedAtActionResult CreatedAtAction(string actionName, object routeValues, [ActionResultObjectValue] object value);
public virtual CreatedAtActionResult CreatedAtAction(string actionName, [ActionResultObjectValue] object value);
public virtual CreatedAtRouteResult CreatedAtRoute(object routeValues, [ActionResultObjectValue] object value);
public virtual CreatedAtRouteResult CreatedAtRoute(string routeName, object routeValues, [ActionResultObjectValue] object value);
public virtual CreatedAtRouteResult CreatedAtRoute(string routeName, [ActionResultObjectValue] object value);
public virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);
public virtual VirtualFileResult File(string virtualPath, string contentType, string fileDownloadName, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag, bool enableRangeProcessing);
public virtual VirtualFileResult File(string virtualPath, string contentType, string fileDownloadName, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag);
public virtual VirtualFileResult File(string virtualPath, string contentType, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag, bool enableRangeProcessing);
public virtual VirtualFileResult File(string virtualPath, string contentType, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag);
public virtual VirtualFileResult File(string virtualPath, string contentType, string fileDownloadName, bool enableRangeProcessing);
public virtual VirtualFileResult File(string virtualPath, string contentType, string fileDownloadName);
public virtual VirtualFileResult File(string virtualPath, string contentType, bool enableRangeProcessing);
public virtual VirtualFileResult File(string virtualPath, string contentType);
public virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag, bool enableRangeProcessing);
public virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag);
public virtual FileStreamResult File(Stream fileStream, string contentType, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag, bool enableRangeProcessing);
public virtual FileStreamResult File(Stream fileStream, string contentType, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag);
public virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName, bool enableRangeProcessing);
public virtual FileStreamResult File(Stream fileStream, string contentType);
public virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag, bool enableRangeProcessing);
public virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag);
public virtual FileContentResult File(byte[] fileContents, string contentType, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag, bool enableRangeProcessing);
public virtual FileContentResult File(byte[] fileContents, string contentType, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag);
public virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName, bool enableRangeProcessing);
public virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName);
public virtual FileContentResult File(byte[] fileContents, string contentType, bool enableRangeProcessing);
public virtual FileContentResult File(byte[] fileContents, string contentType);
public virtual FileStreamResult File(Stream fileStream, string contentType, bool enableRangeProcessing);
public virtual ForbidResult Forbid(params string[] authenticationSchemes);
public virtual ForbidResult Forbid();
public virtual ForbidResult Forbid(AuthenticationProperties properties, params string[] authenticationSchemes);
public virtual ForbidResult Forbid(AuthenticationProperties properties);
public virtual LocalRedirectResult LocalRedirect(string localUrl);
public virtual LocalRedirectResult LocalRedirectPermanent(string localUrl);
public virtual LocalRedirectResult LocalRedirectPermanentPreserveMethod(string localUrl);
public virtual LocalRedirectResult LocalRedirectPreserveMethod(string localUrl);
public virtual NoContentResult NoContent();
public virtual NotFoundObjectResult NotFound([ActionResultObjectValue] object value);
public virtual NotFoundResult NotFound();
public virtual OkResult Ok();
public virtual OkObjectResult Ok([ActionResultObjectValue] object value);
public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag);
public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType, string fileDownloadName, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag, bool enableRangeProcessing);
public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType, string fileDownloadName, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag);
public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType, DateTimeOffset? lastModified, EntityTagHeaderValue entityTag, bool enableRangeProcessing);
public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType, string fileDownloadName, bool enableRangeProcessing);
public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType, string fileDownloadName);
public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType, bool enableRangeProcessing);
public virtual PhysicalFileResult PhysicalFile(string physicalPath, string contentType);
public virtual ObjectResult Problem(string detail = null, string instance = null, int? statusCode = null, string title = null, string type = null);
public virtual RedirectResult Redirect(string url);
public virtual RedirectResult RedirectPermanent(string url);
public virtual RedirectResult RedirectPermanentPreserveMethod(string url);
public virtual RedirectResult RedirectPreserveMethod(string url);
public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName, string fragment);
public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName, object routeValues);
public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName, object routeValues, string fragment);
public virtual RedirectToActionResult RedirectToAction(string actionName);
public virtual RedirectToActionResult RedirectToAction(string actionName, object routeValues);
public virtual RedirectToActionResult RedirectToAction();
public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName);
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName, object routeValues, string fragment);
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName, object routeValues);
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName, string fragment);
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, string controllerName);
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName, object routeValues);
public virtual RedirectToActionResult RedirectToActionPermanent(string actionName);
public virtual RedirectToActionResult RedirectToActionPermanentPreserveMethod(string actionName = null, string controllerName = null, object routeValues = null, string fragment = null);
public virtual RedirectToActionResult RedirectToActionPreserveMethod(string actionName = null, string controllerName = null, object routeValues = null, string fragment = null);
public virtual RedirectToPageResult RedirectToPage(string pageName);
public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler, object routeValues, string fragment);
public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler, string fragment);
public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler, object routeValues);
public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler);
public virtual RedirectToPageResult RedirectToPage(string pageName, object routeValues);
public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler, string fragment);
public virtual RedirectToPageResult RedirectToPagePermanent(string pageName);
public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, object routeValues);
public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler);
public virtual RedirectToPageResult RedirectToPagePermanent(string pageName, string pageHandler, object routeValues, string fragment);
public virtual RedirectToPageResult RedirectToPagePermanentPreserveMethod(string pageName, string pageHandler = null, object routeValues = null, string fragment = null);
public virtual RedirectToPageResult RedirectToPagePreserveMethod(string pageName, string pageHandler = null, object routeValues = null, string fragment = null);
public virtual RedirectToRouteResult RedirectToRoute(string routeName);
public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues);
public virtual RedirectToRouteResult RedirectToRoute(string routeName, string fragment);
public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues, string fragment);
public virtual RedirectToRouteResult RedirectToRoute(object routeValues);
public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, string fragment);
public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues);
public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues, string fragment);
public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName);
public virtual RedirectToRouteResult RedirectToRoutePermanent(object routeValues);
public virtual RedirectToRouteResult RedirectToRoutePermanentPreserveMethod(string routeName = null, object routeValues = null, string fragment = null);
public virtual RedirectToRouteResult RedirectToRoutePreserveMethod(string routeName = null, object routeValues = null, string fragment = null);
public virtual SignInResult SignIn(ClaimsPrincipal principal, string authenticationScheme);
public virtual SignInResult SignIn(ClaimsPrincipal principal, AuthenticationProperties properties);
public virtual SignInResult SignIn(ClaimsPrincipal principal, AuthenticationProperties properties, string authenticationScheme);
public virtual SignInResult SignIn(ClaimsPrincipal principal);
public virtual SignOutResult SignOut();
public virtual SignOutResult SignOut(AuthenticationProperties properties);
public virtual SignOutResult SignOut(params string[] authenticationSchemes);
public virtual SignOutResult SignOut(AuthenticationProperties properties, params string[] authenticationSchemes);
public virtual StatusCodeResult StatusCode([ActionResultStatusCode] int statusCode);
ResponseMessage
的作用,我们怎么能帮助您呢? - Federico DipumaResponseMessage
是来自Asp.Net Web API 2ApiController
将HttpResponseMessage
转换为IHttpActionResult
的。OP正在将之前的代码移植到当前版本。 - NkosiResponseMessage
是System.Web.Http
库中的一个函数部分。它只是根据您指定的HttpResponseMessage
创建一个ResponseMessageResult
。 - Lyco