简体   繁体   中英

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. But 'CreateResponse','RequestUri' and 'CreateErrorResponse' are missing in the Core MVC.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM