CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.11k stars 174 forks source link

Unable to specify custom verbs #269

Closed Dreamwalker666 closed 2 years ago

Dreamwalker666 commented 3 years ago

Currently only the basic HTTP verbs are available but there are 39 total recognised. See here http://webconcepts.info/concepts/http-method/

Currently they can not be used also you can not specify your own custom verbs.

I think there should be a method to do this called Custom which adds the verb in e.g.

Custom("PROPFIND", path, async (request, response) => await response.WriteAsync("Hello World!"));

Thoughts? (I'll pick this up my self but wanted to discuss before hand)

jchannon commented 3 years ago

Sounds good, it’s just whether the underlying MapVerb accepts custom verbs.

On Mon, 9 Nov 2020 at 22:11, Dreamwalker666 notifications@github.com wrote:

Currently only the basic HTTP verbs are available but there are 39 total recognised. See here http://webconcepts.info/concepts/http-method/

Currently they can not be used also you can not specify your own custom verbs.

I think there should be a method to do this called Custom which adds the verb in e.g.

Custom("PROPFIND", path, async (request, response) => await response.WriteAsync("Hello World!"));

Thoughts? (I'll pick this up my self but wanted to discuss before hand)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/269, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJSWTIWFXF44F3Z5SX3SPBSKBANCNFSM4TP47DCA .

Dreamwalker666 commented 3 years ago

I've just been looking at CarterModule.cs and yes it just accepts a string

this.Routes.Add((HttpMethods.Get, path), (handler, conventions));

HttpMethods is just a class has this defined

public static readonly string Get = "GET";

So I think it's doable I'll have a closer look tomorrow :)

jchannon commented 3 years ago

Nice

On Mon, 9 Nov 2020 at 22:22, Dreamwalker666 notifications@github.com wrote:

I've just been looking at CarterModule.cs and yes it just accepts a string

this.Routes.Add((HttpMethods.Get, path), (handler, conventions));

HttpMethods is just a class has this defined

public static readonly string Get = "GET";

So I think it's doable I'll have a closer look tomorrow :)

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/269#issuecomment-724313401, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJXFCS2UUUOM7YXQAJTSPBTQTANCNFSM4TP47DCA .

Dreamwalker666 commented 3 years ago

I tested out on my private repo and it does indeed work fine I added this

protected IEndpointConventionBuilder Custom(string verb, string path, Func<HttpRequest, HttpResponse, Task> handler)
{
    Task RequestDelegate(HttpContext httpContext) => handler(httpContext.Request, httpContext.Response);
    return this.Custom(verb, path, RequestDelegate);
}

protected IEndpointConventionBuilder Custom(string verb, string path, RequestDelegate handler)
{
    path = this.PrependBasePath(path);
    var conventions = new RouteConventions();
    this.Routes.Add((verb, path), (handler, conventions));
    return conventions;
}

public IEndpointConventionBuilder Custom<T>(string verb, string path, Func<HttpRequest, HttpResponse, Task> handler) where T : RouteMetaData
{
    Task RequestDelegate(HttpContext httpContext) => handler(httpContext.Request, httpContext.Response);
    return this.Custom<T>(verb, path, RequestDelegate);
}

protected IEndpointConventionBuilder Custom<T>(string verb, string path, RequestDelegate handler)
    where T : RouteMetaData
{
    path = this.PrependBasePath(path);
    var conventions = new RouteConventions();
    this.Routes.Add((verb, path), (handler, conventions));

    this.RouteMetaData.Add((verb, path), Activator.CreateInstance<T>());
    return conventions;
}

I made 1 public so I could create an extension method

        public static IEndpointConventionBuilder List<T>(this CarterModule module, string path, Func<HttpRequest, HttpResponse, Task> handler)
            where T : RouteMetaData 
            => module.Custom<T>("LIST", path, handler);

Could mean say WebDAV has a separate package with all the correct verbs in for instance?

I'll sort a PR out soon just little swamped :)

jchannon commented 3 years ago

Cool

On Tue, 10 Nov 2020 at 20:25, Dreamwalker666 notifications@github.com wrote:

I tested out on my private repo and it does indeed work fine I added this

protected IEndpointConventionBuilder Custom(string verb, string path, Func<HttpRequest, HttpResponse, Task> handler) { Task RequestDelegate(HttpContext httpContext) => handler(httpContext.Request, httpContext.Response); return this.Custom(verb, path, RequestDelegate); }

protected IEndpointConventionBuilder Custom(string verb, string path, RequestDelegate handler) { path = this.PrependBasePath(path); var conventions = new RouteConventions(); this.Routes.Add((verb, path), (handler, conventions)); return conventions; }

public IEndpointConventionBuilder Custom(string verb, string path, Func<HttpRequest, HttpResponse, Task> handler) where T : RouteMetaData { Task RequestDelegate(HttpContext httpContext) => handler(httpContext.Request, httpContext.Response); return this.Custom(verb, path, RequestDelegate); }

protected IEndpointConventionBuilder Custom(string verb, string path, RequestDelegate handler) where T : RouteMetaData { path = this.PrependBasePath(path); var conventions = new RouteConventions(); this.Routes.Add((verb, path), (handler, conventions));

this.RouteMetaData.Add((verb, path), Activator.CreateInstance<T>());
return conventions;

}

I made 1 public so I could create an extension method

    public static IEndpointConventionBuilder List<T>(this CarterModule module, string path, Func<HttpRequest, HttpResponse, Task> handler)
        where T : RouteMetaData
        => module.Custom<T>("LIST", path, handler);

Could mean say WebDAV has a separate package with all the correct verbs in for instance?

I'll sort a PR out soon just little swamped :)

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/269#issuecomment-724946477, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJS2RY4WXVPY3K23NQLSPGOTLANCNFSM4TP47DCA .

jchannon commented 2 years ago

Carter 6 will now use ASP.NET Core routing directly