how to catch exception in Controller level (Asp.net MVC)
Got a newbie question.
I'm using Asp.net MVC Preview 3 with Spring.net and nHibernate.
Currently, I'm using spring.net to create the controller for me.
And data is persisted to DB through nHibernate at the end of the request which means it perform persistence outside the controller.
How can I catch the exception within the controller.
Cause I want to display the form validation error back to the user and not redirecting them to a generic error page.
one way to solve this, is to wrap your entity manipulation within a transactional service. If your controller calls this service, any data manipulation will already be committed (or aborted), when the call returns to your controller. This will ensure, that your changes are persisted before your controller is left.
You can check out the Spring Northwind example application to see, how this can be done using OpenSessionInView and a Transaction Aspect. It shouldn't make a difference, whether you use ASPX or MVC as your web ui approach as long as you properly layer your architecture.