4

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.

Haacked
  • 58,045
  • 14
  • 90
  • 114
stimms
  • 42,945
  • 30
  • 96
  • 149
  • 1
    This should explain it: [ASP.NET MVC Tip #2 - Redirecting to another action and passing information to it][1] [1]:http://weblogs.asp.net/mikebosch/archive/2008/02/02/asp-net-mvc-tip-2-redirecting-to-another-action-and-passing-information-to-it.aspx – ddc0660 Sep 21 '08 at 16:36

1 Answers1

2

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

Brad Wilson
  • 67,914
  • 9
  • 74
  • 83