In this article I will explain with an example, how to redirect to another View, Page or URL after AJAX call in ASP.Net Core (.Net Core 8) Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 8) Razor Pages, please refer my article ASP.Net Core 8 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

Index PageModel (Code-Behind)

The PageModel consists of following Handler methods.

Handler method for handling GET operation

This Handler method left empty as it is not required.
 

Action method for handling jQuery AJAX operation

This Handler method handles the call made from the jQuery AJAX function from the View.
Note: The following Handler method handles AJAX calls and hence the return type is set to JsonResult.
 

Attributes

ValidateAntiForgeryToken: The ValidateAntiForgeryToken attribute is used to prevent cross-site request forgery attacks.
Note: A cross-site request forgery is an attack is done by sending harmful script element, malicious command, or code from the user’s browser.
 
A Boolean Value True is returned to the View.
public class IndexModel : PageModel
{
    public void OnGet()
    {
    }
 
    [ValidateAntiForgeryToken]
    public IActionResult OnPostAjaxMethod()
    {
        return new JsonResult(true);
    }
}
 
 

Razor Page (HTML)

HTML Markup

The Page consists of an HTML Button. The Button has been assigned a jQuery click event handler and when the Button is clicked a jQuery AJAX call is made to the Handler method.
The Anti-Forgery Token has been added to the Razor Page using the AntiForgeryToken function of the HTML Helper class.
The URL for the jQuery AJAX call is set to the Handler method of the Razor PageModel i.e. /Index?handler=AjaxMethod. The returned response is received in the Success event handler and the page is redirected to another View, Page or URL.
@page
@model Redirect_After_AJAX_Call_Core_Razor.Pages.IndexModel
@{
    Layout = null;
}
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @Html.AntiForgeryToken()
    <input type="button" id="btnRedirect" value="Redirect" />
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnRedirect").click(function () {
                $.ajax({
                    type: "POST",
                    url: "/Index?handler=AjaxMethod",
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("XSRF-TOKEN",
                            $('input:hidden[name="__RequestVerificationToken"]').val());
                    },
                    data: {},
                    success:function (response) {
                        if (response == true) {
                            alert("You will now be redirected.");
                            window.location "https://www.aspsnippets.com/";
                        }
                    }
                });
           });
        });
    </script>
</body>
</html>
 
 

Screenshot

ASP.Net Core Razor Pages: Redirect to another View (URL) after AJAX call
 
 

Downloads