How to Access a Controller from Startup.cs

lesponce 176 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();  
                
}  
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
Developer technologies | C#
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    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 176 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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.