-1

I have gone through this solution but this is not what I am looking for get parameters from URL in controller constructor.

Let's say, In an ASP.NET Core Controller I'm requesting to some methods. Let's say the controller is something like:

public class ClassroomsController : Controller {

    private string requestedUserId;

    // C O N S T R U C T O R
    public ClassroomsController()
    {
        /**** THIS POINT ***/
        requestedUserId = ;
    }

    [HttpGet("Classrooms/API01/{userId}")]
    public IActionResult API01(string userId)
    {

    }

    [HttpGet("Classrooms/API02/{userId}")]
    public IActionResult API02(string userId)
    {

    }
}

What I want is whether I request in Classrooms/API02 or Classrooms/API01 the requestedUserId should be initiated inside the constructor based on the parameter userId in both API01 and API02;

Airn5475
  • 2,452
  • 29
  • 51
Shunjid Rahman
  • 409
  • 5
  • 17
  • 1
    How do you expect to populate the `requestedUserId` value if it's not passed in? – DavidG Nov 22 '19 at 13:17
  • If this is to implement some sort of authentication/authorization, then use that instead of working around the normal way. – Tanveer Badar Nov 22 '19 at 13:17
  • 4
    at the time of controller construction, there is **no request** from which you can get the URL. what you want to do is logically impossible. would be helpful to know *why* you want to do this, because it seems you're trying to solve another problem with an impracticable method. – Franz Gleichmann Nov 22 '19 at 13:17
  • As we know whether we are requesting to a controller, the constructor is first executed. That' why I am asking here if there is any possible way to do it. @DavidG – Shunjid Rahman Nov 22 '19 at 13:20
  • @FranzGleichmann Is there no possible way to catch the URL in constructor? :'( – Shunjid Rahman Nov 22 '19 at 13:21
  • @DavidG I had to do some common functionalities with the userId in each API. Yes I can optimize them by putting them into some functions. But my instructor told me to call them once in the constructor instead of calling the function in every API's. This made me confused :'( – Shunjid Rahman Nov 22 '19 at 13:28
  • Constructor happends before anything else. Take an house, it's possible to know the name of someone that enter by the front door in the constructor. Because there is no house nor door, we are building those. And each time you use the constructor you get a new house. So there is no point to try to pass the human that used the door to an other constructor to see the builder build the house around him. – xdtTransform Nov 22 '19 at 13:45
  • @Shunjid no. you cannot have the url before the request, and you cannot have the request before the controller handling it. also: a controller instance is *shared* between requests - so *which* url would you even want to have? from which request? – Franz Gleichmann Nov 22 '19 at 15:03

1 Answers1

2

What I want is whether I request in Classrooms/API02 or Classrooms/API01 the requestedUserId should be initiated inside the constructor based on the parameter userId in both API01 and API02

Short Answer: Not Possible.

All request related logic needs to happen within the action itself.

public class ClassroomsController : Controller {

    // C O N S T R U C T O R
    public ClassroomsController() {
        //...
    }

    [HttpGet("Classrooms/API01/{userId}")]
    public IActionResult API01(string userId) {
        string requestedUserId = userId;

        //...
    }

    [HttpGet("Classrooms/API02/{userId}")]
    public IActionResult API02(string userId) {
        string requestedUserId = userId;

        //...
    }
}

If there is some common functionality that needs to be invoked, then move that to a method that can be called by the controller actions when invoked.

Nkosi
  • 235,767
  • 35
  • 427
  • 472