My controller has abstract base controller. I want to access the form post data inside abstract base class constructor. How can we do that ?
public abstract class AppController : Controller
{
public AppController()
{
// request post data required here
}
}
public class ProductController : AppController
{
public ProductController() { }
}
Purpose : Updating second dropdown on change of first dropdown. Both are on MASTER page.
Code given above is one of the 2 options to pass data to master page:
- Add using ViewData in ALL the action methods.
- Do it in only one place using abstract base controller – add the required data using ViewData inside its constructor and make our main controller class implement this abstract base controller class. So that we don’t have to add the viewdata for master page in all action methods.
I don’t know what is your final goal with this but this is something which is not recommended to be done in MVC. The Request object is not yet initialized in the constructor of the controller. You could try to use the native
HttpContextobject:but that’s something extremely bad and I would never recommend you doing this as now your controller is coupled to the static native HttpContext instance without any chance of unit testing it.
Instead of using the constructor you could override the
Initializemethod of your controller where you will have access to the request context and you could read posted data: