Base Controller

General information

"BaseController.cs" is the class that provides the basic methods that all Controllers need. Thus, whenever a Controller is created, it needs to inherit from "BaseController.cs".

Methods Overview

In the following, these methods and their functionality will be explained:

OnActionExecuting

OnActionExecuting is a method in the ASP.NET Core MVC framework that is called before an action method is executed. It is used to perform any necessary logic or validation before the action method is executed, such as authorization or authentication. It takes an ActionExecutingContext object (information about the current request and action) as parameter.

If the OnActionExecuting method determines that the request is invalid or should not be processed, it can set the Result property of the ActionExecutingContext object to a different ActionResult to prevent the action method from being executed. If no action result is set in the OnActionExecuting method, the action method will be executed as normal.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);
}

OnException

OnException is a method in the ASP.NET Core MVC framework that is called when an unhandled exception occurs during the processing of a request. It is used to handle the exception and provide a response to the client.

The OnException method typically takes an ExceptionContext object as a parameter, which contains information about the exception that occurred and the context in which it occurred. This method can be used to log the exception, handle it in some way, or provide a custom response to the client.

If the OnException method sets the ExceptionHandled property of the ExceptionContext object to true, it indicates that the exception has been handled and no further action is required. If the ExceptionHandled property is not set or is set to false, the framework will continue to process the exception as usual, which may result in an error page being displayed to the client.

It's important to note that OnException is only called for unhandled exceptions that occur within an MVC controller action method. Exceptions that occur outside of an action method, such as in middleware or other parts of the application, will not be handled by this method.

protected override void OnException(ExceptionContext filterContext)
{
    base.OnException(filterContext);

    var model = filterContext.Controller.ViewData["HttpException"];

    if (model != null)
    {
        //return error page
        filterContext.Result = PartialView("~/Views/Home/Error.cshtml", model);
    }

    //confirm that error is handled
    filterContext.ExceptionHandled = true;
}

Dispose

The Dispose method is used to release any resources used by an object and perform any necessary cleanup before the object is destroyed. It is commonly used in .NET applications to ensure that unmanaged resources, such as file handles, database connections, or network sockets, are properly released when they are no longer needed. When the boolean parameter "disposing" is set to false, it will only release those unmanaged resources. If it is set to true, it will additionally release managed resources.

It's important to note that the Dispose method should always be called when an object that implements IDisposable is no longer needed. This can be done explicitly by calling the Dispose method on the object, or by using a using statement to automatically dispose of the object when it goes out of scope. Failing to properly dispose of objects that use unmanaged resources can result in resource leaks and degraded performance.

protected override void Dispose(bool disposing)
{
    if (MiddleContext.UnitWork != null)
    {
        MiddleContext.UnitWork.Dispose();
        base.Dispose(disposing);
    }
}

RenderRazorViewToString

RenderRazorViewToString is a method in ASP.NET Core MVC framework that is used to render a Razor view to a string. The rendered string can then be used in various ways, such as sending it as the body of an email, saving it to a file, or passing it to another process, which can be useful in a variety of scenarios, such as generating email templates, PDF documents, or other types of content that can be sent as a string.

It takes two parameters:

  1. A string parameter that specifies the name of the view to be rendered.

  2. An object parameter that provides any necessary data to the view. This parameter is optional and can be null if the view does not require any data.

        public string RenderRazorViewToString(string viewName, object model)
        {
            ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);

                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);

                return sw.GetStringBuilder().ToString();
            }
        }

Last updated