HttpContext is null

Friday, December 27, 2024 8:02:40 PM
  • Posted: Wednesday, July 21, 2021 2:11 PM
  • 17
I need to call controllers methods from my plugin. I can call them and this is my code:

AccountController accountController = new AccountController(_authenticationService, _translationService, _workContext, _customerService, _groupService, _userFieldService, _customerManagerService, _countryService, _mediator, _messageProviderService, _captchaSettings, _customerSettings);

await accountController.Register(model, returnUrl, captchaValid, form, customerAttributeParser);


But a runtime error happen which say "HttpContext" is null. (in AccountController.cs Line 486)

please help me to fix it.
thanks
0
  • Posted: Wednesday, July 21, 2021 2:12 PM
  • 17
Mohsen wrote:
I need to call controllers methods from my plugin. I can call them and this is my code:

AccountController accountController = new AccountController(_authenticationService, _translationService, _workContext, _customerService, _groupService, _userFieldService, _customerManagerService, _countryService, _mediator, _messageProviderService, _captchaSettings, _customerSettings);

await accountController.Register(model, returnUrl, captchaValid, form, customerAttributeParser);


But a runtime error happen which say "HttpContext" is null. (in AccountController.cs Line 486)

please help me to fix it.
thanks


Gandnode v2 latest version
0
  • Posted: Thursday, July 22, 2021 9:21 AM
  • Team
  • 153
Controllers are to respond to requests at certain routes, so you shouldn't create objects based on controller.
I would like to suggest you to try run something like this:


var serviceProvider = HttpContext.RequestServices;
var mycontroller =(MyController)serviceProvider.GetRequiredService<MyController>();
mycontroller.SomeMethod();
1
back to top
Filters