重定向到其他控制器

时间:2020-03-06 14:30:33  来源:igfitidea点击:

我在IAuthorizationFilter中有一些代码,该代码将用户重定向到登录页面,但是我无法更改所使用的控制器。所以我可能会做

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

但这导致

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

我正在从产品控制程序运行此程序。如何使视图引擎使用用户控件而不是产品控件?

编辑:我与它一起工作

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

但这是一种污点,我敢肯定有更好的方法。令人沮丧的是,ActionFilterAttribute中几乎没有暴露。如果在AuthorizationContext中公开的控制器让RedirectToAction公开,这似乎很有用,这很容易。

解决方案

同意ddc0660,我们应该重定向。不要运行u.Login(),而是将context.Result设置为RedirectResult。