Controller constructor does not get called
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
add a comment |
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
add a comment |
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
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
http asp.net-core routing
edited Nov 13 '18 at 6:39
Bercovici Adrian
asked Nov 12 '18 at 15:52
Bercovici AdrianBercovici Adrian
1,0941816
1,0941816
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
1
Does it matter if the controllerclass
is public or not ?
– Bercovici Adrian
Nov 12 '18 at 20:23
According to theIsController
'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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
1
Does it matter if the controllerclass
is public or not ?
– Bercovici Adrian
Nov 12 '18 at 20:23
According to theIsController
'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
add a comment |
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.
1
Does it matter if the controllerclass
is public or not ?
– Bercovici Adrian
Nov 12 '18 at 20:23
According to theIsController
'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
add a comment |
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.
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.
edited Nov 12 '18 at 19:18
answered Nov 12 '18 at 18:39
kasptomkasptom
10815
10815
1
Does it matter if the controllerclass
is public or not ?
– Bercovici Adrian
Nov 12 '18 at 20:23
According to theIsController
'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
add a comment |
1
Does it matter if the controllerclass
is public or not ?
– Bercovici Adrian
Nov 12 '18 at 20:23
According to theIsController
'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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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