简体   繁体   中英

Handle POST and GET in C#

I'm trying to develop a program that handles POST and GET requests.

I've spent countless hours searching around the web for a tutorial that doesn't depend on ASP.NET I do not want to use ASP.NET just standard C# .

How can I achieve this? The furthest I've gotten to is this:

if (HttpMethod.ContentType == "POST") {
  // Code here
}

I've made a function HttpListen server on http://localhost:8080/ which sends a response.

What I'm looking for is you do http://localhost:8080/?method=val1&results=val2 .

Thanks, Brown.

You're looking for an HTTP server library that is not ASP.NET?

Awkwardly bypassing the question "What's wrong with ASP.NET?"....

Nancy is an awesome lightweight open source library. You can find some samples on github , although the samples are a bit on the heavy side. If you're looking for an extremely basic setup you can get away with a couple dozen lines of code. A good example is the console-based self-hosted sample.

// add such a module class to your assembly, Nancy should find it automagically
public class ResourceModule : NancyModule
{
    public ResourceModule() : base("/products")
    {
        // would capture routes to /products/list sent as a GET request
        Get["/list"] = parameters => {
            return "The list of products";
        };
    }
}

// then start the host
using (var host = new NancyHost(new Uri("http://localhost:1234")))
{
   host.Start();
   // do whatever other work needed here because once the host is disposed of the server is gone 
   Console.ReadLine();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM