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();
    }
}




Monday, October 10, 2022

How to implement securities in Asp.NET MVC.

 


https://www.c-sharpcorner.com/UploadFile/cda5ba/security-feature-in-mvc/

How to Handle Exception in Asp.NET MVC

 5 Ways to do MVC Error Handling

  1. Web.Config customErrors
  2. MVC HandleErrorAttribute
  3. Controller.OnException method
  4. HttpApplication Application_Error event
  5. Collect exceptions via .NET profiling with Retrace.

There are two critical things that you need accomplish with error handling:

  1. Gracefully handling errors and show your users a friendly error page
  2. Logging errors so that you are aware of them and can monitor them

  1. Web.Config customErrors:

<system.web>
    <customErrors mode="On" defaultRedirect="~/ErrorHandler/Index">
        <error statusCode="404" redirect="~/ErrorHandler/NotFound"/>
    </customErrors>
<system.web/>


There are several ways to do MVC error handling. You should always specify a default error page via your web.config <customErrors> and log unhandled exceptions that get called back to your HttpApplication Error method.

You can use HandleErrorAttribute or OnException to provide fine-grained control of how you display error type messages to your users.

If you want to track all of your application exceptions, be sure to check our Retrace and our error monitoring features. You can also view all application exceptions on your workstation for free with our free profiler, Prefix.




Saturday, October 8, 2022

When to Use filter with real time example and Types.

 The ASP.NET MVC framework supports four different types of filters:


  1.  Authorization filters – Implements the IAuthorizationFilter attribute.
  2. Action filters – Implements the IActionFilter attribute.
  3. Result filters – Implements the IResultFilter attribute.
  4. Exception filters – Implements the IExceptionFilter attribute.

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.



What is HTTP Handler and HTTP Module.

HTTP module and HTTP handler are used by MVC to inject pre-processing logic in the request chain.

  • HTTP Handlers are extension based pre-processor whereas HTTP Module are event based preprocessor.
    • For example: if you want to change how 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.


    • If you want your functionality to only be executed once per Http Request, you should use an HttpModule.



Saturday, August 6, 2022

Using TempData, Peek And Keep In ASP.NET MVC

  •  If you set value for TempData and do not read the value then the data will be available for next request.
  • If you set value for TempData and read in View then the data will be deleted or will be null.

  • If you read TempData in the first request and want to keep the value for the next request then use 'Keep' Method.
  • If you read the TempData using 'Peek' then value persists for the next request also. 

You can use Peek() when you always want to hold/prevent the value for another request. You can use Keep() when prevent/hold the value depends on additional logic.