Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Improvements over MapVerb and friends... #5295

Closed
atrauzzi opened this issue Sep 19, 2016 · 10 comments
Closed

Improvements over MapVerb and friends... #5295

atrauzzi opened this issue Sep 19, 2016 · 10 comments
Labels

Comments

@atrauzzi
Copy link

atrauzzi commented Sep 19, 2016

They're close to being useful, but I feel like they are still limited in application. What is needed is a way to define a name, the template, the controller and the action without having to resort to the anonymous types every time.

Right now in one of my projects, I define a set of extension methods as follows:

        public static IRouteBuilder Get(
            this IRouteBuilder routes,
            string path,
            string controller,
            string action,
            string name,
            object constraints = null
        ) {
            return routes.AddVerb("GET", path, controller, action, name, constraints);
        }

My AddVerb is similar in intention to MapVerb, except that it uses MapRoute so that I can gain the benefit of route names:

        private static IRouteBuilder AddVerb(
            this IRouteBuilder routes,
            string httpMethod,
            string path,
            string controller,
            string action,
            string name,
            object constraints = null,
            object defaults = null
        ) {

            if(defaults == null)
                defaults = new {
                    controller,
                    action,
                };

            if(constraints == null)
                constraints = new RouteValueDictionary(new {
                    httpMethod = new HttpMethodRouteConstraint(httpMethod)
                });

            return routes.MapRoute(name, path, defaults, constraints);

        }

What's missing here compared to MapVerb is:

  • I can still pass a controller and action rather than a RequestDelegate
  • I can still name the route

I'm not sure if this is because the Map* series is an abstraction at the middleware level and MapRoute is an abstraction at the MVC level. But I've campaigned in the past (1, 2) for a better API for route declaration. I think this is still an area where ASP.net Core can improve greatly.

High level suggestions would be:

  • Do not rely on conventions
  • Do not force people to use attributes
  • Do not rely on areas to influence application surface area
  • Better documentation ;)
@Eilon
Copy link
Member

Eilon commented Oct 4, 2016

Though we are not planning to add these extension methods to the system, we recommend that if you want to build this approach that instead of building "conventional routes" (i.e. MapRoute), that this instead use MVC's Application Model and Conventions to create attribute routes. They are both less error prone and can have better performance. Note: This doesn't mean using attribute routes anywhere in the code of the application - rather, it's using the underlying attribute routing system to compute the route logic.

@Eilon Eilon closed this as completed Oct 4, 2016
@Eilon Eilon added the wontfix label Oct 4, 2016
@atrauzzi
Copy link
Author

atrauzzi commented Oct 5, 2016

@Eilon - Would you have an example of what my touchpoints would be for that? Even just a quick example?

Thanks!

@rynowak
Copy link
Member

rynowak commented Oct 5, 2016

This is a good hello world example of dynamically configuring routing.

https://github.com/aspnet/Entropy/tree/dev/samples/Mvc.CustomRoutingConvention

Let us know if there are specific things you need help with

@atrauzzi
Copy link
Author

atrauzzi commented Oct 5, 2016

Again, not sure I follow the advice here. I don't think what I'm suggesting is a convention.

I specifically avoid attribute routing. I understand it's a feature you guys are trying to push, but it's not ideal.

@Eilon
Copy link
Member

Eilon commented Oct 5, 2016

@atrauzzi this isn't about actually using attribute routing. For your scenario it just happens to be that some types have the term "attribute routing" in them, but none of your controllers or anything would use attribute routing.

@atrauzzi
Copy link
Author

atrauzzi commented Oct 5, 2016

Got ya. The example linked seemed to be about creating a convention. Which I don't think I'd be doing?

@mmacneil
Copy link

mmacneil commented Sep 8, 2017

@atrauzzi - Were you using this with MVC Routing ie. app.UseMvc() or were you using purely custom routing?

@atrauzzi
Copy link
Author

atrauzzi commented Sep 8, 2017

@mmacneil - On top of the MVC routing engine.

I've come up with some conventions to get cleaner routing in my application, you can see the extensions here: https://github.com/atrauzzi/praxis/tree/master/Praxis/Routing

This more or less is what I was looking for - although I don't doubt the ASP.net team could improve on it. While I haven't really documented this library, it is being used if you're interested. It's just a jumble of ideas & conventions I wanted for ASP.net Core projects.

Unfortunately, only for 1.x. Haven't gotten into 2.x much.

@mmacneil
Copy link

mmacneil commented Sep 8, 2017

Thanks @atrauzzi ! I'll check it out. I hit the same sort of constraint with the MapVerb, MapGet etc. apis and used your AddVerb extension as part an attribute-less routing approach.

Thanks,
Mark

@atrauzzi
Copy link
Author

atrauzzi commented Sep 8, 2017

Yeah, I definitely wasn't looking to reinvent the wheel. Just that the current MVC APIs for routing really pale in comparison to other frameworks out there.

Glad I could help! :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants