Search code examples
c#asp.net-core

ASP.NET Core redirect from POST action to url doesn't work


I call a POST method in react but at the end I want to get redirected to the root url of my application.

I have this code where I'm trying to redirect to the root url (localhost:50000) , but it doesn't do anything

I also tried doing Redirect("http://localhost:50000") and it also doesn't work. I tried testing with google.com and I get a serverside error, basically I can't redirect to any website.

[Route("api/[controller]")]
[ApiController]
public class EntityBuilderController : ControllerBase
{

    [HttpPost("[action]")]
    public ActionResult CreateEntity(string xmlPath, string dllPath)
    {
        DataMapper.ExtractEntities(xmlPath, dllPath);

        return Redirect(Url.Content("~/"));
    }

}

Solution

  • Update: after seeing the questioner original code through remote access I have found that questioner application is a ASP.NET Core React SPA.

    You cannot redirect from API controller to your client app view. You have to do the redirection from your client app on successful entity creation.