ASP.net MVC provides various options to map the http request (browser request or form data) view’s data to model properties via controller action method.
- Form Collection
- Model binding
ASP.net MVC provides various options to map the http request (browser request or form data) view’s data to model properties via controller action method.
Let's create a custom exception filter to log every unhandled exception by deriving the built-in HandleErrorAttribute class and overriding the OnException method, as shown below.
You can now apply the MyErrorHandler attribute at the global level or controller or action method level, the same way we applied an HandleError attribute.
class MyErrorHandler : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
Log(filterContext.Exception);
base.OnException(filterContext);
}
private void Log(Exception exception)
{
//log exception here..
}
}You can now apply the MyErrorHandler attribute at the global level orcontroller or action method level, the same way we applied an HandleError attribute.
[MyErrorHandler] public class HomeController : Controller { public ActionResult Index() { return View(); } }
5 Ways to do MVC Error Handling
<system.web> <customErrors mode="On" defaultRedirect="~/ErrorHandler/Index"> <error statusCode="404" redirect="~/ErrorHandler/NotFound"/> </customErrors> <system.web/>
The ASP.NET MVC framework supports four different types of filters:
Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter.
Authorization filters are used to implement authentication and authorization for controller actions. For example, the Authorize filter is an example of an Authorization filter.
Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns.
Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You also can use exception filters to log errors.
HTTP module and HTTP handler are used by MVC to inject pre-processing logic in the request chain.
jpg files are processed, you will implement custom HTTP handler versus if you want to execute additional logic during processing of the request, you will implement a custom HTTP module. There is always only one HTTP handler for a specific request but there can be multiple HTTP modules.