3

I am trying to create little program which is going to act like a web server and accepts url parameters. I have found this example project: https://codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx

Is there any way to make this receive my url parameters? Is there any other example project like this which has this functionality?

3 Answers 3

4

You should take a look at OWIN/Katana. Based on your question – and with OWIN's ability to be hosted in any process – this might fit quite well and is the current way to go:

http://www.asp.net/aspnet/overview/owin-and-katana/getting-started-with-owin-and-katana

You'll find tons of sample on this topic. For your question related to parameters you could refer to this article.

Based on the first link you could do something like the follwing:

public class Startup1
{
    public void Configuration(IAppBuilder app)
    {
        app.Run(context =>
        {
            var value = context.Request.Query.Get("someKey");

            if (value == "foo")
            {
                // do something
            }

            context.Response.ContentType = "text/plain";
            return context.Response.WriteAsync("Hello, world.");
        });
    }
}

A request could look like this: http://someServer:80/?someKey=foo

Sign up to request clarification or add additional context in comments.

2 Comments

Well, this OWIN / Katana project looks promising. I can confirm that what @Shahrooz linked in the other answer works, and I am sure this one does as well. But I am not closer to get those parameters. Could you please be more 'specific'?
See my updated answer. Further research is up to you ;)
4

Please read this article: Building A Simple File Server With OWIN and Katana

class Program
    {
        static void Main(string[] args)
        {
            var url = "http://localhost:8080";
            WebApp.Start(url, builder => builder.UseFileServer(enableDirectoryBrowsing:true));            
            Console.WriteLine("Listening at " + url);
            Console.ReadLine();
        }
    }

2 Comments

Could you please be a little more specific on how to implement this into my app?
@Zoszko you can go to this article: odetocode.com/blogs/scott/archive/2014/02/10/…
0

Based on your project example try to put in method SendRequest as follow:

string name = request.QueryString.Get("name");
            switch (name){
                case "a":
                    return string.Format("<HTML><BODY>My web page a.<br>{0}</BODY></HTML>", DateTime.Now);
                    break;
                case "b":
                    return string.Format("<HTML><BODY>My web page b.<br>{0}</BODY></HTML>", DateTime.Now);
                    break;
                default:
                    return string.Format("<HTML><BODY>My web page .<br>{0}</BODY></HTML>", DateTime.Now);
                    break;
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.