Reputation: 117
I have a non-controller file called MyLib.cs where I have a method where, for a given condition, I want it to redirect to another page.
I used RedirectToAction()
, but I got an error saying it dose not exist in current context.
Any ideas what I should use?
Upvotes: 2
Views: 974
Reputation: 9242
I think you can use Response.Redirect here. Consider this code:
HttpContext.Current.Response.Redirect("YOUR_PAGE_Virtual_PATH");
//example: http://www.mywebsite.com/home/list
Upvotes: 3
Reputation: 623
This is against some of the MVC tenets, you're breaking the Separation of Concerns concept, only the controllers should return views, not your libs. I can give you two suggestions:
In your lib return a Enum, and them the controller would figure out which view should be displayed.
If not all paths from your lib causes a redirection, throw an exception and the controller handles it redirecting to the appropriate view.
Upvotes: 1
Reputation: 3201
I assume MyLib is called from within a controller?
Could you set variable in MyLib.cs and redirect from your controller based on that once MyLib has finished?
Update :
Do you absolutely have to redirect from this class? I say this because this is going against the MVC paradigm. You should really be handling all your routing within your controllers. Sticking to it really will make your application far more maintainable. Really can't stress that enough! :)
Upvotes: 0