-1

I have an ASP.NET Core project and I need to ask an input to the developer with a Console.ReadLine() at the project launch but it's not working. When the Console.ReadLine() is hit, it sends me to the local webpage that the project makes appear when the project is launched.

I already tried to create a Console Application project where I do the Console.ReadLine() and that I call in the ASP.NET project with a process.start() but it has exactly the same behavior and just sends me to the webpage, I see that the application is waiting something when Console.ReadLine is hit but I don't have the usual console to write an input.

I don't have a lot of code to show, it is a simple Console.ReadLine() and I don't have any exceptions.

Do you know how should I do to simply have a console where i can give an input in an asp core project ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    What do you mean by "sends me to the web page"? When you launch an ASP.NET Core app in Visual Studio, usually it does launch a browser as part of that launch, but that's got nothing to do with you having Console.ReadLine. More fundamentally though, why are you using Console.ReadLine in a server application? If you need configuration information, I'd very strongly encourage you to use the normal ASP.NET Core configuration system for that. – Jon Skeet Nov 14 '20 at 08:12
  • You might have project types confused. See whether the console project template is better suited for you. – stefan Nov 14 '20 at 08:17
  • No this is the right type of project, it's a micro service which handles the database migration and i need a console application to ask to developpers if the database connexion string which they are using when launching the project is the correct one. And by the web page i mean like you said Jon, the browser which is launched at the start of the project – dev_paon Nov 14 '20 at 08:35
  • The goal is to avoid a mistake and always do a migration on the right database because db migration is automated. – dev_paon Nov 14 '20 at 08:47
  • What do you expect to happen if your application is being run on a server somewhere with no console attached? Again, this really sounds like *configuration* which should be handled via the normal ASP.NET Core configuration. Or if you really want it to be interactive, don't make it a service. – Jon Skeet Nov 14 '20 at 08:49
  • Okay I guess you are right. Thank you for your answers – dev_paon Nov 14 '20 at 08:57

1 Answers1

2

You simply cannot use Console.ReadLine() in an web oriented application. The fact that the Console class is available, is related to the .net framework, but not to the project type.

IAmNotARobot
  • 139
  • 15
  • Well you *can* use it - but it would be very odd to do so. If you launch the ASP.NET Core app directly (rather than via IIS Express) you can indeed interact with the console. It's just a really bad idea to do so. – Jon Skeet Nov 14 '20 at 08:48
  • @JonSkeet That's interesting! Could you please explain _why_ it is such a bad idea (performance, security... ?) and _how_ reading the console input could be done if one decides to go that way? Thanks! – KarloX Nov 15 '20 at 15:27
  • 2
    @KarloX: It's a bad idea because web applications are generally intended to be run *non-interactively* whereas a console is interactive. Reading from the console if you really, really want to do it is just a matter of calling `Console.ReadLine` as normal... but it won't be any use if you don't *have* a console, of course... – Jon Skeet Nov 15 '20 at 15:54