scottoffen / grapevine

Fast, unopinionated, embeddable, minimalist web framework for .NET
https://scottoffen.github.io/grapevine/
MIT License
103 stars 16 forks source link

How to create a route with both GET and POST method ? #73

Closed pubpy2015 closed 3 years ago

pubpy2015 commented 3 years ago

In Grapevine V4, i can do that:

[RestRoute(PathInfo = @"/")]
public IHttpContext ROOT(IHttpContext context)
{
    context.Response.SendResponse(Encoding.UTF8.GetBytes("Welcome to Grapevine"));
    return context;
}
seikosantana commented 3 years ago

I saw this article: https://scottoffen.github.io/grapevine/docs/routes

If the method is omitted, the route is assigned the default method HttpMethod.Any, which, as the name suggests, will match any HTTP method. We could use "Any" or HttpMethod.Any enum when specifying the route. I guess that would work. Since that matches any methods, if you want it to specifically match only GET and POST methods, maybe could do some branching in the method assigned to the route to check the method from the context.

Hope it helps.

pubpy2015 commented 3 years ago

Thank you very much.

scottoffen commented 3 years ago

You can also use multiple Route attributes on the method.

[RestRoute("Get", "/hello")]
[RestRoute("Post", "/hello")]
public async Task HelloWorld(IHttpContext context)
{
    await context.Response.SendResponseAsync($"Hello, world!");
}

However, you would have to differentiate in the method if you want the behavior to change depending on whether the method is POST or GET.

pubpy2015 commented 3 years ago

Thanks,

An other question:

I am building a server on my PC (IP address of my PC is: 192.168.100.92) as:

string _serverPort = "1122"; var server = RestServerBuilder.UseDefaults().Build(); server.Prefixes.Add($"http://localhost:{_serverPort}/"); server.Start();

but when trying access to http://127.0.0.1:1122/api/test or http://192.168.100.92:1122/api/test i get the result is: ảnh

only when access to http://localhost:1122/api/test i get the corect result: ảnh

How to solve this problem ? On your V4, I can do all with: server.Host = "+"; server.Port = 1122; server.Start();

pubpy2015 commented 3 years ago

Oh, seem replace "localhost" by "+" will solve this problem: server.Prefixes.Add($"http://+:{_serverPort}/");