How to Access a Controller from Startup.cs

lesponce 156 Reputation points
2022-11-08T14:19:29.61+00:00

I’m trying to access a controller from the Startup.cs class. This is a .NET Core application running with Angular for the frontend. Can you please advise me how I can access the controller from the Startup class?

I need to execute some code before

public async Task<IActionResult> InitiateProcess(string returnUrl)  
{  
  var hello = _configuration[ConfigSetting];  
  await = _ServiceProvider.InitiateJobAsync(hello, returnUrl);  

  return new EmptyResult();  
                
}  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
3,161 questions
ASP.NET MVC
ASP.NET MVC
A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
853 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,209 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 36,261 Reputation points
    2022-11-08T16:10:17.633+00:00

    At startup there is no request or http context, which are typically injected and used by the controller. What ever code you want to call should be refactored to a library call from both the controller and startup.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. lesponce 156 Reputation points
    2022-11-08T15:09:59.637+00:00

    The application should use the controller so it call an API for authentication. Once the authentication is authorized, the external tool calls another method.

    All what I need for now is to call the method listed on the question which is what needs to be called when the application is started.