[英]ASP.NET Core.Http.HttpRequest does not contain definition for 'CreateResponse','RequestUri' and 'CreateErrorResponse'
I am trying to convert below code from ASP.Net MVC to ASP.Net Core MVC.我正在尝试将以下代码从 ASP.Net MVC 转换为 ASP.Net Core MVC。 But 'CreateResponse','RequestUri' and 'CreateErrorResponse' are missing in the Core MVC.但是核心 MVC 中缺少“CreateResponse”、“RequestUri”和“CreateErrorResponse”。
public HttpResponseMessage CallUserMappings()
{
try
{
//return response status as successfully created with entity
var msg = Request.CreateResponse(HttpStatusCode.Created);
//Response message with requesturi for check purpose
msg.Headers.Location = new Uri(Request.RequestUri.ToString());
return msg;
}
catch (Exception ex)
{
//return response as bad request with exception message.
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
enter image description here在此处输入图像描述
Is there any alternative approach for these?有没有其他方法可以解决这些问题?
This would be an untested equivalent in .NET Core:这将是 .NET Core 中未经测试的等效项:
public IActionResult CallUserMappings()
{
try
{
return Created(Request.GetDisplayUrl(), null);
}
catch (Exception ex)
{
return BadRequest(ex);
}
}
To use the Request.GetDisplayUrl()
method, you need to import the Microsoft.AspNetCore.Http.Extensions
namespace.要使用Request.GetDisplayUrl()
方法,您需要导入Microsoft.AspNetCore.Http.Extensions
命名空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.