tags:

views:

696

answers:

2

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.

+3  A: 

This should explain it.

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
That works inside a controller but not in an ActionFilterAttribute.
stimms
+1  A: 

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

Brad Wilson