5

Is there any difference between:

  1. app.MapGroup("") vs app.MapGroup("/")
  2. app.MapGet("task") vs app.MapGet("/task")

When I did tests, they looked no different. Just want to confirm. If they are the same, why do we add the "/"?

2 Answers 2

3

Always take an initiative and dive in the source code =). When you have a challenge or need to understand why something works some way.

The difference is that if you append a "/" for anything calling MapMethods i.e MapGroup,MapGet, MapPost,MapPatch...etc.It will be trimmed.

The RoutePatternFactoryClass will trim it under the hood i.e the first "/" or these two characters "~/" (with RoutePatternParser under method TrimPrefix()) the resultant string will be taken as the route pattern.

why do we add the "/"?

I would guess majorly because of the templates we just adopt it not questioning why? ,from examining source I see this will be an overhead (though minimal and trivial), I would just skip those characters and put my intended path i.e .MapGet("task").

Sign up to request clarification or add additional context in comments.

Comments

0

While both forms might appear to behave similarly in many cases, especially in web development to use leading slashes in route definitions, making the code more readable and consistent.

This is a preference, but I would prefer to use slash as a prefix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.