首页 > 解决方案 > Redirect to a new URL using C#/JavaScript and logout

问题描述

I have an Action Result, that I want to use to pass a new URL to redirect to via JS and LOGOUT from the site. What is the best way to approach this?

if (!ModelState.IsValid)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json(new { Errors = ModelState.Errors() });
            }
            else
            {
                    return Json(new { newUrl = "http://www.google.com" });     
                }
                else
                {
                    return Json(new UpdateResponse() { Success = true });
                }
            }     

UPDATE REDIRECT LINK HERE

vmCartChangeAccount.ApplyResponse = function () {
            if (!app.ajaxService.inCriticalSection()) {
                app.ajaxService.criticalSection(true);
                app.ajaxService.ajaxPostJson("@Url.Action("ChangeAccountOrLocation", "Cart")",
                        ko.mapping.toJSON(vmCartChangeAccount.Form),
                        function (result) {
                            // redirect to NEW URL
                            app.ajaxService.criticalSection(false);
                            window.location.replace(**REPLACETORUL HERE**);
                        }, function (result) {
                            vmCartChangeAccount.Form.ClearCart(false);
                            app.doneLoading();
                            app.ajaxService.criticalSection(false);
                            $("#error-modal").modal();
                        }, vmCartChangeAccount.ModalErrors);
            }
        };

标签: javascriptc#model-view-controller

解决方案


通过这样做,您可以重定向到您在 startup.cs 文件中配置的 HomeController

  <li class="nav-item">
            <form class="form-inline" asp-area="Test" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new {area = ""})">
                <button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
            </form>
        </li>

推荐阅读