Converting C# console application to HTML for browsers?

Stefan Walczak 1 Reputation point
2022-04-29T21:27:35.177+00:00

Hello, I tried finding my exact solution before asking but I have a console app I designed that is simply text-based and uses if-else statements and loops (or keeps accepting input and responds) until the user types “no” or “no thank you”. Basically a chatbot.

I couldn’t find if there was any way to make this console app viewable on a website with html? Basically the page load would start the program and the viewer could play around with the program and ask questions on my website. If there was a way to format a console window box that I could size to my liking for my website that would be ideal. Thanks in advance if anyone has any valuable advice!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,129 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,746 Reputation points
    2022-04-29T22:02:37.147+00:00

    Have you looked at websockets?

    They're used to create persistent two-way connections between a client & server:
    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-6.0

    Here's a full working example of an .NET MVC app with a websocket enabled:
    https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/websockets/samples/6.x/WebSocketsSample

    When the user visits the site they're served up wwwroot/index.html which establishes a websocket connection with websocket server endpoint listening on the /ws URL on the backend.
    The server then awaits messages from the client in a loop & echos the same message back to the client:

    https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/websockets/samples/6.x/WebSocketsSample/Controllers/WebSocketController.cs#L27

    That's where your chatbot can take over.

    0 comments No comments

  2. Bruce (SqlWork.com) 68,311 Reputation points
    2022-04-29T22:04:13.777+00:00

    you can use the <pre></pre> markup to match console output and <input> to input. the big difference is that the web sites are stateless. that is your console app code would work like it exited after every response. the app would need to save the state for the next run.

    typically with a website you store a state key (or entire state if small) in a cookie. the browser will send this cookie on each request. you use the cookie info to restore state.

    the other issues is that website are multi-threaded, so you can not use static variables to save state (unless its indexed by a user key). also a hosted website may be on several servers so state needs to be available from any server (typically a database or network cache)

    a modern option to store state at user level is to use web sockets. this is a persistent connection, so you can tie state to the connection. signal/r Is library for this, and used for chat style apps.

    0 comments No comments

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.