For redirecting from one view to other view from the controller:
To be specific , you should not include any action logic into view.appropriate way is you should write the logic in controller action method. use RedirectToAction(string actionName,string controllerName) & in the destination action method
return the view using View() which you want to redirect.
if you want to redirect to an error page as stated in your title, the right way is by throwing an exception. Then, the logic you have in place to handle exceptions will do the job(either pages configured in the configuration file, or HandleError ActionFilter
logic).
prashant kha...
Member
18 Points
16 Posts
How to redirect to error page in view?
Jun 24, 2011 12:41 PM|LINK
Hi Guys,
I am new to Asp.NET MVC 2
How can i redirect from Asp.Net MVC page to existing .asp page .
Any help would be greatly appriciated.
Thanks in Advance.
ignatandrei
All-Star
134929 Points
21622 Posts
Moderator
MVP
Re: How to redirect to error page in view?
Jun 24, 2011 01:47 PM|LINK
usually , the business logic should stay in the controller . From there you can redirect to a page with Server.transfer
If you want in aspx page, the you can do with javascript
window.location
techieshrava...
Member
18 Points
6 Posts
Re: How to redirect to error page in view?
Jun 24, 2011 02:01 PM|LINK
For redirecting from one view to other view from the controller:
To be specific , you should not include any action logic into view.appropriate way is you should write the logic in controller action method. use RedirectToAction(string actionName,string controllerName) & in the destination action method return the view using View() which you want to redirect.
http://theshravan.net
francesco ab...
All-Star
20912 Points
3279 Posts
Re: How to redirect to error page in view?
Jun 24, 2011 03:04 PM|LINK
if you want to redirect to an error page as stated in your title, the right way is by throwing an exception. Then, the logic you have in place to handle exceptions will do the job(either pages configured in the configuration file, or HandleError ActionFilter logic).
Mvc Controls Toolkit | Data Moving Plug-in Videos
Forest Cheng...
Star
8370 Points
819 Posts
Re: How to redirect to error page in view?
Jun 27, 2011 03:35 AM|LINK
Hi Prashant,
If you want to redirect in controller, there are Redirect and RedirectToAction can be used.
If you want to redirect in the other places, HttpContext.Current.Response.Redirect(string url) can be used.
If you want to implement custom error handler, look at these links: http://community.codesmithtools.com/CodeSmith_Community/b/tdupont/archive/2011/03/01/error-handling-and-customerrors-and-mvc3-oh-my.aspx
http://deanhume.com/Home/BlogPost/custom-error-pages-in-mvc/4
Hope this helpful,
Forest Cheng
If you have any feedback about my replies,please contact msdnmg@microsoft.com.
Microsoft One Code Framework
guinnesslee
Participant
1050 Points
197 Posts
Re: How to redirect to error page in view?
Jun 27, 2011 11:07 AM|LINK
<customErrors mode="On" defaultRedirect="CustomErrorView" >
<error statusCode="404" redirect="Error/Error404" />
<error statusCode="500" redirect="Error/Error500" />
</customErrors>
The controller:
This could be done with a static page if needed:
<customErrors mode="On" >
<error statusCode="404" redirect="Static404.html" />
</customErrors>
twitter
www.mashupweb.wordpress.com