javascript - 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);
}
};
解决方案
通过这样做,您可以重定向到您在 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>
推荐阅读
- powershell - 使用 Powershell 脚本创建任务计划程序
- python - 如何删除“。” 和字符串中的“-”?
- python - 使用TCP同时在两个进程之间进行多处理连接
- python - 需要深入了解为什么我的 Python 函数没有正确比较字符串
- java - 如何在 Nifi 中使用表达式语言定义 ControllerService
- r - PDF 中未出现特殊字符
- javascript - 将相同的 api 数据加载到 div 和模态对话框中
- python - 在 macOS 上使用 pip 设置安装独角兽的库路径(缺少 libunicorn.dylib)
- powershell - 列出目录中最后修改的子目录
- sql - 具有多个联接、计数、分组依据的对 DAX(在 Power BI 中)的 SQL 查询