Nothing happens when Redirect(url) is executed.

Volk Volk 551 Reputation points
2023-02-11T07:32:54.8033333+00:00
Hi!

I'm trying to call the async method before return View() so that the heavy method request does not interfere with the appearance of the View, but works a little later (so that inscriptions appear on the screen that the process needs 1-3 seconds to execute). Everything is fine, but when the asynchronous method performs its task, I call Redirect(url) in it, but it does not work - just silence, no errors, silence like in a crypt. I tried return Redirect(url), HttpContext.Response.Redirect(url), RedirectToAction("Action", url) - nothing helps - nothing happens.

The browser does not redirect me to another https page. Why? This is a simple action and I need it, because I get a certain protected token-link to a protected token-page from the outside in an asynchronous method - and there is nothing terrible about it.

How do I solve this problem?

I tried to save return values in static - it doesn't help. I tried calling the Redirect method without return. - it doesn't help. Nothing helps! Why? It's a simple action.

Thanks!




public async Task<IActionResult> Method(Params params)

{            

	AsyncDo(params);

	return View(params); // The View is shown! Good!

}

private async Task<IActionResult> AsyncDo(Params params)

{

	ResultAsync resultAsync = await Task.Run(() =>

            {

                return WebAPI.LongHardWebRequest(params);

            });

	// This is triggered after 1-3 seconds

	return Redirect(resultAsync.url); // It doesn't work!

	HttpContext.Response.Redirect(resultAsync.url); // It doesn't work!

 	return RedirectToAction("RedirectTo", resultAsync.url); // It doesn't work!

	return View();

}

// The method is never called!

public IActionResult RedirectTo(string url)

{

	return Redirect(url);

	return View();

}

public async Task<IActionResult> Method(Params params)

{            

    AsyncDo(params);

    return View(params);

}















ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,779 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 29,786 Reputation points
    2023-02-11T15:05:21.22+00:00

    Then give me an example of how I can block the current View() in the browser or close it with a transparent background with the inscription "Waiting..." so that a person does not click on all the buttons during a long request?

    JavaScript to the recuse...

    The script toggles the CSS display property which hides the form and shows the Waiting prompt. The response from the Method action replaces the page content.

    @{
        ViewData["Title"] = "Home Page";
    }
    
    <div class="text-center">
        <div id="myform">
            <form method="post" action="/Home/Method">
                <input type="submit" name="submit" value="submit" />
            </form>
        </div>
        <div id="waiting" style="display:none;"> 
            Waiting...
        </div>
    
    </div>
    
    @section scripts {
        <script>
            addEventListener('submit', (event) => {
                document.getElementById("myform").style.setProperty("display", "none");
                document.getElementById("waiting").style.removeProperty("display")
            });
        </script>
    }
    
    [HttpGet]
    public IActionResult Index()
    {
        return View();
    }
    
    [HttpPost]
    public async Task<IActionResult> Method()
    {
        await AsyncDo();
        return View();
    }
    
    private async Task AsyncDo()
    {
        await Task.Delay(1000 * 3);
    }
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Takahito Iwasa 4,851 Reputation points MVP
    2023-02-11T12:40:44.7966667+00:00

    Hi, @Volk Volk

    Is it the Method(Params params) that the request is processed by?

    I think you need to receive and return the return of AsyncDo.

    What if you do:

    public async Task<IActionResult> Method(Params params)
    {            
    	return await AsyncDo(params);
    }
    

  2. AgaveJoe 29,786 Reputation points
    2023-02-11T13:48:38.9466667+00:00

    You misunderstand how HTTP works. HTTP is a request/response protocol.

    • A TCP connection is opened.
    • The client sends a request.
    • The server responds.
    • The TCP connection is closed.

    Your design expects two responses. One that returns the View from the Method action. Then a second response from AsyncDo. The way the code is designed, the Method action allows the main thread to run straight though the method body since there is not await. Therefore, the View is returned and the connection is closed. At a later time, the AsyncDo method completes. At this point there's no connection and there's no code waiting for the completion.

    The easiest path forward is to await AsyncDo and accept it takes a few second for AsyncDo to complete.

    If you want to do something when the AsyncDo completes then design must make multiple requests to check the state of the AsyncDo processes. AsyncDo can store the process state in a database or memory cache. The Method action will return a unique Id to client so the client can find where AsyncDo stores the state.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.