dotnet / WatsonWebserver

Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
MIT License
411 stars 84 forks source link

Serving Content #125

Closed john144 closed 11 months ago

john144 commented 11 months ago

I am new to the Watson Webserver. I am working through the Test.Routing and Test.Default examples, and having trouble figuring out how to code a request in the browser that would display content. I see where the routes are added, but I don't see any handlers for them. Are there built-in default handlers? Do I need to write a handler? Ultimately, I would like to serve web pages that reference javascript, images and style sheets. Thanks.

            _Server.Routes.PreAuthentication.Content.Add("/html/", true);
            _Server.Routes.PreAuthentication.Content.Add("/large/", true);
            _Server.Routes.PreAuthentication.Content.Add("/img/watson.jpg", false);
jchristn commented 11 months ago

Great catch @john144 - the Routing.ContentHandler must be assigned. I'll work to update the projects and documentation.

In the meantime, you can instantiate ContentRouteHandler and map that to Routing.ContentHandler for the default handler.

jchristn commented 11 months ago

Hi @john144 here's how I amended the Test.Default project: image

e.g.

_Server.Routes.PreAuthentication.Content.Add("/html/", true);
_Server.Routes.PreAuthentication.Content.Add("/large/", true);
_Server.Routes.PreAuthentication.Content.Add("/img/watson.jpg", false);
_Server.Routes.PreAuthentication.ContentHandler = new ContentRouteHandler(_Server.Routes.PreAuthentication.Content);

Will push this to github in a moment!

jchristn commented 11 months ago

BTW, I'm updating to v6.1.0 and moving the handler into the ContentRouteManager to make this much easier. It can still be overloaded.

jchristn commented 11 months ago

Done, please give it a go w/ v6.1.1.

john144 commented 11 months ago

Thanks a lot for the fix. I was able to get both the JPG and the HTML working, then I added a large HTML file of my own. The images and style also rendered as expected. What solved it was your fix, your example, and the fact that these sub-folders must be in the same folder as the executable.

On Mon, Nov 13, 2023 at 3:39 PM Joel Christner @.***> wrote:

Done, please give it a go w/ v6.1.1.

— Reply to this email directly, view it on GitHub https://github.com/dotnet/WatsonWebserver/issues/125#issuecomment-1809170137, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABRKEYVNEG6BRAH27NGKDC3YEKHQFAVCNFSM6AAAAAA7JTOBPKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMBZGE3TAMJTG4 . You are receiving this because you were mentioned.Message ID: @.***>

jchristn commented 11 months ago

Awesome! You can also use explicit paths instead of relative paths if you want to hold the files elsewhere

john144 commented 11 months ago

I looked through the server code and figured out how to change the BaseDirectory. Also, how to do a redirect, so I can just say "/help", and it gets redirected to wherever the help page is located.

I just filed an issue I found with the latest version: ContentHandler missing from Version 6.1.1

Incidentally, in my search for an HTTP server to include with my C# project, only Watson Webserver turned up in my search. I don't know much about web servers or how they work, but it was easy for me to search through the source code and find what I needed. I haven't changed any server code. I just wanted to out how to do something. My project is now mostly working, but I needed to rewrite it into a WinForms app.

Also, I have been using your DatabaseWrapper for my SQLite database. It's easy to use and works great. Thanks for all the packages you have written.

On Mon, Nov 13, 2023 at 8:33 PM Joel Christner @.***> wrote:

Awesome! You can also use explicit paths instead of relative paths if you want to hold the files elsewhere

— Reply to this email directly, view it on GitHub https://github.com/dotnet/WatsonWebserver/issues/125#issuecomment-1809447180, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABRKEYVQZPOL7BCJE7DWPADYELKAFAVCNFSM6AAAAAA7JTOBPKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMBZGQ2DOMJYGA . You are receiving this because you were mentioned.Message ID: @.***>

jchristn commented 11 months ago

Thanks @john144 for the kind words! I'm glad to hear the libraries are working out for you. Cheers, Joel