safe-routing icon indicating copy to clipboard operation
safe-routing copied to clipboard

Ideas for generated routes

Open austins opened this issue 8 months ago • 4 comments

Awesome project! Would these features be possible in the realm of source generators?

💡 Idea 1: Handler method type Assuming the handler method name conventions can't be changed in ASP.NET, if the method name starts with OnGet, add a property on the generated IPageRouteValues implementation, e.g. public HttpMethod Method => HttpMethod.Get; (or the HttpMethods.Get for a string type). Same for OnPost, etc.

One use case would be creating a tag helper for HTMX, which adds attributes for URLs like hx-get, hx-post. The tag helper can take an attribute such as hx-for-route and then map it to the attribute with the corresponding method along with any query/route values.

~~💡 Idea 2 (maybe): Relative path~~ ~~Currently, there is a way to get PageName, but not the "relative path".~~

~~I wrote an extension method for use outside of views. Not sure if it'll be useful within the source generated class or not. It looks like:~~

    public static string GetPagePath(this IPageRouteValues routeValues)
    {
        const string indexPagePath = "/Index";
        if (routeValues.PageName == indexPagePath)
        {
            return "/";
        }

        var pagePath = routeValues.PageName;
        if (routeValues.PageName.EndsWith(indexPagePath, StringComparison.Ordinal))
        {
            pagePath = routeValues.PageName[..routeValues.PageName.LastIndexOf(
                indexPagePath,
                StringComparison.Ordinal)];
        }

        return pagePath.ToLowerInvariant(); // Maybe not everyone has the lowercase routes settings enabled?
    }

~~Example mappings of page name -> relative path would be:~~

  • ~~"/Index" => "/"~~
  • ~~"/Auth/Login/Index" => "/auth/login"~~
  • ~~"/Admin/Posts" => "/admin/posts"~~

austins avatar Jun 07 '24 05:06 austins