本文介绍了重定向到不同的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个IAuthorizationFilter用户重定向到一个登录页面,一些code,但我无法改变它用于控制器。所以,我可能会做

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;
 }

但是,这将导致

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

我从一个产品CONTROLER运行此。我如何使用用户CONTROLER而不是产品的CONTROLER视图引擎?

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?


编辑:我得到了它的工作。

I got it working with

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

但是,这是一个cludge,我敢肯定有一个更好的办法。有令人沮丧的ActionFilterAttribute小露。好像如果AuthorizationContext暴露控制器有RedirectToAction暴露,这将是很容易,可能是有用的。

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.

推荐答案

同意ddc0660,你应该重定向。不要运行u.Login(),而是集context.Result到RedirectResult。

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

这篇关于重定向到不同的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 06:22