[英]c# mvc aspnet.core async Task<IActionResult>, RedirectToAciton not working
I am using MVC aspnetcore dotnetcore 6.0我正在使用 MVC aspnetcore dotnetcore 6.0
I do an authorization with a LoginController.我使用 LoginController 进行授权。 When it is success i want to redirect the user into HomeController.当它成功时,我想将用户重定向到 HomeController。 But RedirectToAction does not work.但是 RedirectToAction 不起作用。 My code is here:我的代码在这里:
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> UserLoginAsync(LoginRequestModel requestModel)
{
try
{
if (ModelState.IsValid)
{
LoginResponseModel loginResponseModel = new LoginResponseModel();
var res = _wrapperService.Post<LoginRequestModel, LoginResponseModel>(requestModel, "/api/auth/login");
var tokenRes = GetTokenInfo(res.Result.Data.Token);
HttpContext.Session.SetString("username", requestModel.Email);
var userIdentity = new ClaimsIdentity(tokenRes, "Login");
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(userIdentity);
await HttpContext.SignInAsync(claimsPrincipal);
return RedirectToAction("Index", "Home"); // I want to redirect this but it does not work.
return Ok(res.Result);
}
return BadRequest(ModelState);
}
catch (Exception e)
{
var errors = new HttpErrorViewModel()
{
Errors = new List<string>()
};
errors.Errors.Add(e.InnerException.Message);
return BadRequest(errors);
}
}
My Ajax code here:我的 Ajax 代码在这里:
function LoginWithMail() {
var inputs = {};
$("#form-login .input-login").each(function () {
inputs[$(this)[0].name] = $(this)[0].value;
});
$.ajax({
method: "POST",
url: "/Login/UserLogin/",
data: inputs,
dataType: 'json',
beforeSend: function () {
$("#Email").removeClass("border border-danger");
$("#Password").removeClass("border border-danger");
$("#LoginSpinner").show();
$("#loginResMessages").html("").removeClass();
},
success: function (res) {
console.log(res);
$("#LoginSpinner").hide();
},
error: function (xhr, status, error) {
$("#LoginSpinner").hide();
$("#loginResMessages").html("");
I repeat.我重复。 I want to redirect this code when it is success.我想在成功时重定向此代码。 I tried many solutions i found but not worked too.我尝试了许多我找到的解决方案,但也没有奏效。 Please help.请帮忙。
Did you send request to login action with ajax?您是否使用 ajax 发送了登录操作请求? It can be a problem.这可能是个问题。
If did you send post request with ajax, just handle the response inside ajax method.如果您使用 ajax 发送 post 请求,只需在 ajax 方法中处理响应。 If it is success, change the window location to home/index with js.如果成功,用js将window位置改为home/index。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.