ASP.Net Core MVC RedirectToAction将控制器名称附加在returnUrl之前 [英] ASP.Net Core MVC RedirectToAction is appending controller name in front of returnUrl

查看:178
本文介绍了ASP.Net Core MVC RedirectToAction将控制器名称附加在returnUrl之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ASP.Net核心MVC 6应用程序.我有一个AppControler作为我的初始控制器,如果用户尝试执行具有[Authorize]属性的操作,我将重定向到我的AuthController进行登录,并传入returnUrl作为返回值.一旦通过身份验证,我将使用... return RedirectToAction(returnUrl).

I am working on a ASP.Net core, MVC 6 application. I have an AppControler as my initial controller and if a user tries to go to an action that has an [Authorize] attribute, I redirect to my AuthController for the login, passing in the returnUrl for the return. Once authenticated, I use ... return RedirectToAction(returnUrl).

在调试中,我可以看到returnUrl字符串设置为/App/Timesheets.但是,在浏览器地址栏中,它的地址为 http://localhost:49940/Auth/%2FApp%2FTimeSheets .由于某种原因,它会将控制器名称(Auth)附加在returnUrl之前.

In debug, I can see that the returnUrl string is set to /App/Timesheets. However, in the browser address bar, it has an address of http://localhost:49940/Auth/%2FApp%2FTimeSheets. For some reason it is appending the controller name (Auth) in front of the returnUrl.

这是我在AuthController中的登录操作

Here is my Login action in the AuthController

    [HttpPost]
    public async Task<ActionResult> Login(LoginViewModel vm, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var signInResult = await _signInManager.PasswordSignInAsync(vm.Username, vm.Password, true, false);

            if (signInResult.Succeeded)
            {
                if (string.IsNullOrWhiteSpace(returnUrl))
                {
                    return RedirectToAction("Timesheets", "App");
                }
                else
                {
                    return RedirectToAction(returnUrl);
                }
            }
            else
            {
                ModelState.AddModelError("", "Username or password incorrect");
            }
        }

        return View();
    }

我可以在浏览器地址中手动输入 http://localhost:49940/App/Timesheets 并进入正确的视图.另外,如果我添加

I can manually enter http://localhost:49940/App/Timesheets in the browser address bar and get to the correct view. Also, if I add

returnUrl = String.Empty;  //Test Only

在一行之前...

if (string.IsNullOrWhiteSpace(returnUrl))

使其执行该行...

return RedirectToAction("Timesheets", "App");

重定向工作正常.因此,这与问题所在的"/Controller/Action"格式的字符串变量的传递有关.

The redirection works just fine. So it has something to do with passing in a string variable in the "/Controller/Action" format that is the problem.

有什么想法吗?

推荐答案

如果已经具有完整的URL,则应返回Redirect.当前,您正在执行RedirectToAction,它将尝试重定向到当前控制器(Auth)下的操作.

When you have a full URL already, you should return a Redirect. Currently you are doing a RedirectToAction which will try to redirect to an action under the current controller (Auth).

if (signInResult.Succeeded)
{
   if (string.IsNullOrWhiteSpace(returnUrl))
   {
      return RedirectToAction("Timesheets", "App");
   }
   else
   {
      return Redirect(returnUrl);
   }
}

这篇关于ASP.Net Core MVC RedirectToAction将控制器名称附加在returnUrl之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆