Controller constructor does not get called










0















Hello i am trying to understand why do my requests not enter my api route.They seem to reach the server but they wont fan out in the MVC.



The server is running on: http://localhost:9300

The route i am requesting is : http://localhost:9300/api/getusers

Program





public class Program 
public static void Main(string args)
CreateWebHostBuilder(args).Build().Run();


public static IWebHostBuilder CreateWebHostBuilder(string args)
var builder = new WebHostBuilder();
builder.UseStartup<Startup>();
var url = Address.Default.ToUrl();
builder.UseKestrel().UseUrls(url);
return builder;






Startup



public class Startup 

public Startup(IConfiguration configuration)
Configuration = configuration;

public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.AddMvc();

public IConfiguration Configuration;
public void Configure(IApplicationBuilder app)

Debug.WriteLine("Entered server"); //enters successfully here
app.UseMvc(); //does not enter the controller




Controller



This is a simple controller with a GET method.The constructor is not invoked at all.Why would this happen?I know it when the server runs the first time ..it does a health check on its routes.



[ApiController]
class UserController : ControllerBase

private static List<User> users = new List<User>
new UserId=0,Age=0,Name="Failed",
new UserId=12,Age=33,Name="Daniel",
new UserId=13,Age=33,Name="Marian",
;

public UserController()
Debug.WriteLine("Controller called"); //does not get called !

[HttpGet]
[Route("api/getusers")]
public async Task<HttpResponseMessage> GetUsers()

await Task.Delay(1000);
return new HttpResponseMessage
Content = new StringContent(users.ToJson()),
StatusCode = HttpStatusCode.OK
;




P.S Do i have to add anyything ? What am i missing i followed other implementations closely.










share|improve this question




























    0















    Hello i am trying to understand why do my requests not enter my api route.They seem to reach the server but they wont fan out in the MVC.



    The server is running on: http://localhost:9300

    The route i am requesting is : http://localhost:9300/api/getusers

    Program





    public class Program 
    public static void Main(string args)
    CreateWebHostBuilder(args).Build().Run();


    public static IWebHostBuilder CreateWebHostBuilder(string args)
    var builder = new WebHostBuilder();
    builder.UseStartup<Startup>();
    var url = Address.Default.ToUrl();
    builder.UseKestrel().UseUrls(url);
    return builder;






    Startup



    public class Startup 

    public Startup(IConfiguration configuration)
    Configuration = configuration;

    public void ConfigureServices(IServiceCollection services)
    services.AddOptions();
    services.AddMvc();

    public IConfiguration Configuration;
    public void Configure(IApplicationBuilder app)

    Debug.WriteLine("Entered server"); //enters successfully here
    app.UseMvc(); //does not enter the controller




    Controller



    This is a simple controller with a GET method.The constructor is not invoked at all.Why would this happen?I know it when the server runs the first time ..it does a health check on its routes.



    [ApiController]
    class UserController : ControllerBase

    private static List<User> users = new List<User>
    new UserId=0,Age=0,Name="Failed",
    new UserId=12,Age=33,Name="Daniel",
    new UserId=13,Age=33,Name="Marian",
    ;

    public UserController()
    Debug.WriteLine("Controller called"); //does not get called !

    [HttpGet]
    [Route("api/getusers")]
    public async Task<HttpResponseMessage> GetUsers()

    await Task.Delay(1000);
    return new HttpResponseMessage
    Content = new StringContent(users.ToJson()),
    StatusCode = HttpStatusCode.OK
    ;




    P.S Do i have to add anyything ? What am i missing i followed other implementations closely.










    share|improve this question


























      0












      0








      0


      0






      Hello i am trying to understand why do my requests not enter my api route.They seem to reach the server but they wont fan out in the MVC.



      The server is running on: http://localhost:9300

      The route i am requesting is : http://localhost:9300/api/getusers

      Program





      public class Program 
      public static void Main(string args)
      CreateWebHostBuilder(args).Build().Run();


      public static IWebHostBuilder CreateWebHostBuilder(string args)
      var builder = new WebHostBuilder();
      builder.UseStartup<Startup>();
      var url = Address.Default.ToUrl();
      builder.UseKestrel().UseUrls(url);
      return builder;






      Startup



      public class Startup 

      public Startup(IConfiguration configuration)
      Configuration = configuration;

      public void ConfigureServices(IServiceCollection services)
      services.AddOptions();
      services.AddMvc();

      public IConfiguration Configuration;
      public void Configure(IApplicationBuilder app)

      Debug.WriteLine("Entered server"); //enters successfully here
      app.UseMvc(); //does not enter the controller




      Controller



      This is a simple controller with a GET method.The constructor is not invoked at all.Why would this happen?I know it when the server runs the first time ..it does a health check on its routes.



      [ApiController]
      class UserController : ControllerBase

      private static List<User> users = new List<User>
      new UserId=0,Age=0,Name="Failed",
      new UserId=12,Age=33,Name="Daniel",
      new UserId=13,Age=33,Name="Marian",
      ;

      public UserController()
      Debug.WriteLine("Controller called"); //does not get called !

      [HttpGet]
      [Route("api/getusers")]
      public async Task<HttpResponseMessage> GetUsers()

      await Task.Delay(1000);
      return new HttpResponseMessage
      Content = new StringContent(users.ToJson()),
      StatusCode = HttpStatusCode.OK
      ;




      P.S Do i have to add anyything ? What am i missing i followed other implementations closely.










      share|improve this question
















      Hello i am trying to understand why do my requests not enter my api route.They seem to reach the server but they wont fan out in the MVC.



      The server is running on: http://localhost:9300

      The route i am requesting is : http://localhost:9300/api/getusers

      Program





      public class Program 
      public static void Main(string args)
      CreateWebHostBuilder(args).Build().Run();


      public static IWebHostBuilder CreateWebHostBuilder(string args)
      var builder = new WebHostBuilder();
      builder.UseStartup<Startup>();
      var url = Address.Default.ToUrl();
      builder.UseKestrel().UseUrls(url);
      return builder;






      Startup



      public class Startup 

      public Startup(IConfiguration configuration)
      Configuration = configuration;

      public void ConfigureServices(IServiceCollection services)
      services.AddOptions();
      services.AddMvc();

      public IConfiguration Configuration;
      public void Configure(IApplicationBuilder app)

      Debug.WriteLine("Entered server"); //enters successfully here
      app.UseMvc(); //does not enter the controller




      Controller



      This is a simple controller with a GET method.The constructor is not invoked at all.Why would this happen?I know it when the server runs the first time ..it does a health check on its routes.



      [ApiController]
      class UserController : ControllerBase

      private static List<User> users = new List<User>
      new UserId=0,Age=0,Name="Failed",
      new UserId=12,Age=33,Name="Daniel",
      new UserId=13,Age=33,Name="Marian",
      ;

      public UserController()
      Debug.WriteLine("Controller called"); //does not get called !

      [HttpGet]
      [Route("api/getusers")]
      public async Task<HttpResponseMessage> GetUsers()

      await Task.Delay(1000);
      return new HttpResponseMessage
      Content = new StringContent(users.ToJson()),
      StatusCode = HttpStatusCode.OK
      ;




      P.S Do i have to add anyything ? What am i missing i followed other implementations closely.







      http asp.net-core routing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 6:39







      Bercovici Adrian

















      asked Nov 12 '18 at 15:52









      Bercovici AdrianBercovici Adrian

      1,0941816




      1,0941816






















          1 Answer
          1






          active

          oldest

          votes


















          4














          I've created the webapi project using dotnet new webapi.
          I've managed to get to the url with the similar configuration by changing the access modifier of a similar controller. Try to add public keyword to the class UserController. So it should be public class UserController



          I will provide more information about the configuration of the project if it is necessary and the step above does not help.






          share|improve this answer




















          • 1





            Does it matter if the controller class is public or not ?

            – Bercovici Adrian
            Nov 12 '18 at 20:23












          • According to the IsController's implementation, yes. However, you can try to adjust the proposed workaround if you have the reason to not make it public.

            – kasptom
            Nov 12 '18 at 20:49











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265677%2fcontroller-constructor-does-not-get-called%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          I've created the webapi project using dotnet new webapi.
          I've managed to get to the url with the similar configuration by changing the access modifier of a similar controller. Try to add public keyword to the class UserController. So it should be public class UserController



          I will provide more information about the configuration of the project if it is necessary and the step above does not help.






          share|improve this answer




















          • 1





            Does it matter if the controller class is public or not ?

            – Bercovici Adrian
            Nov 12 '18 at 20:23












          • According to the IsController's implementation, yes. However, you can try to adjust the proposed workaround if you have the reason to not make it public.

            – kasptom
            Nov 12 '18 at 20:49
















          4














          I've created the webapi project using dotnet new webapi.
          I've managed to get to the url with the similar configuration by changing the access modifier of a similar controller. Try to add public keyword to the class UserController. So it should be public class UserController



          I will provide more information about the configuration of the project if it is necessary and the step above does not help.






          share|improve this answer




















          • 1





            Does it matter if the controller class is public or not ?

            – Bercovici Adrian
            Nov 12 '18 at 20:23












          • According to the IsController's implementation, yes. However, you can try to adjust the proposed workaround if you have the reason to not make it public.

            – kasptom
            Nov 12 '18 at 20:49














          4












          4








          4







          I've created the webapi project using dotnet new webapi.
          I've managed to get to the url with the similar configuration by changing the access modifier of a similar controller. Try to add public keyword to the class UserController. So it should be public class UserController



          I will provide more information about the configuration of the project if it is necessary and the step above does not help.






          share|improve this answer















          I've created the webapi project using dotnet new webapi.
          I've managed to get to the url with the similar configuration by changing the access modifier of a similar controller. Try to add public keyword to the class UserController. So it should be public class UserController



          I will provide more information about the configuration of the project if it is necessary and the step above does not help.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 19:18

























          answered Nov 12 '18 at 18:39









          kasptomkasptom

          10815




          10815







          • 1





            Does it matter if the controller class is public or not ?

            – Bercovici Adrian
            Nov 12 '18 at 20:23












          • According to the IsController's implementation, yes. However, you can try to adjust the proposed workaround if you have the reason to not make it public.

            – kasptom
            Nov 12 '18 at 20:49













          • 1





            Does it matter if the controller class is public or not ?

            – Bercovici Adrian
            Nov 12 '18 at 20:23












          • According to the IsController's implementation, yes. However, you can try to adjust the proposed workaround if you have the reason to not make it public.

            – kasptom
            Nov 12 '18 at 20:49








          1




          1





          Does it matter if the controller class is public or not ?

          – Bercovici Adrian
          Nov 12 '18 at 20:23






          Does it matter if the controller class is public or not ?

          – Bercovici Adrian
          Nov 12 '18 at 20:23














          According to the IsController's implementation, yes. However, you can try to adjust the proposed workaround if you have the reason to not make it public.

          – kasptom
          Nov 12 '18 at 20:49






          According to the IsController's implementation, yes. However, you can try to adjust the proposed workaround if you have the reason to not make it public.

          – kasptom
          Nov 12 '18 at 20:49


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265677%2fcontroller-constructor-does-not-get-called%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20