Monday, April 17, 2023

Introduction To ASP.NET MVC 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.

  1. Form Collection
  2. Model binding


Model Binding:

Model class create krte h then pass the parameter to the Action method.


Friday, March 31, 2023

Create Custom Action Filter in MVC


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 or
controller or action method level, the same way we applied an HandleError attribute.

[MyErrorHandler]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}