Question:
Why it is must to use a forward slash in the ASP.NET Core API?

Problem:

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 "/"?


Solution:

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").


Answered  by: >Qiang Fu

Credit: >Stack Overflow


Suggested blogs:

>How to route between different components in React.js?

>How to save python yaml and load a nested class?-Python

>How to send multiple HTML form fields to PHP arrays via Ajax?

>What is data binding in Angular?

>What is microservice architecture, and why is it better than monolithic architecture?

>What is pipe in Angular?

>What makes Python 'flow' with HTML nicely as compared to PHP?

>What to do when MQTT terminates an infinite loop while subscribing to a topic in Python?

>Creating custom required rule - Laravel validation


Nisha Patel

Nisha Patel

Submit
0 Answers