Redirect to different controller

2019-06-26 06:35发布

I have some code in an IAuthorizationFilter which redirects the user to a login page but I'm having trouble changing the controller which is used. So I might do

public void OnAuthorization(AuthorizationContext context)
{
  UserController u = new UserController();
  context.Result = u.Login();
  context.Cancel = true;
 }

But this results in

The view 'Login' or its master could not be found. The following locations were searched:
~/Views/Product/Login.aspx
~/Views/Product/Login.ascx
~/Views/Shared/Login.aspx
~/Views/Shared/Login.ascx

I am running this from a product controler. How do I get the view engine to use the user controler rather than the product controler?


Edit: I got it working with

RedirectResult r = new RedirectResult("../User.aspx/Login");
context.Result = r; 
context.Cancel = true;

But this is a cludge, I'm sure there is a better way. There is frustratingly little exposed in the ActionFilterAttribute. Seems like it might be useful if the controller exposed in AuthorizationContext had RedirectToAction exposed this would be easy.

标签: asp.net-mvc
1条回答
女痞
2楼-- · 2019-06-26 06:58

Agree with ddc0660, you should be redirecting. Don't run u.Login(), but rather set context.Result to a RedirectResult.

查看更多
登录 后发表回答