Redirect to another controller

I have code in the IAuthorizationFilter that redirects the user to the login page, but I am having problems changing the controller used. Therefore i could do

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

But this leads to

 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 run this from the product controller. How to force the viewer to use a user controller rather than a product controller?


Edit: I worked with

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

But this is a treasure, I am sure that there is a better way. There is disappointment in the ActionFilterAttribute. It seems that it would be useful if the dispatcher opened in the AuthorizationContext had RedirectToAction open, that would be easy.

+4
source share
1 answer

Agree with ddc0660, you have to redirect. Do not run u.Login (), but set the context. Return to RedirectResult.

+2
source

Source: https://habr.com/ru/post/1276988/


All Articles