stimms
stimms

Reputation: 44094

Redirect to different controller

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.

Upvotes: 4

Views: 10839

Answers (1)

Brad Wilson
Brad Wilson

Reputation: 70736

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

Upvotes: 2

Related Questions