Home > Mobile >  c# ASP.NET - API Controllers - should Route start with "/" or not?
c# ASP.NET - API Controllers - should Route start with "/" or not?

Time:06-02

I have a remedial question. In our code base, I'm seeing different API controllers and sometimes, devs are using routes that start with "/" and other times not.

From what I can tell, regardless of whether the endpoint starts with "/" or not, they all are discoverable by this same URI

 https://localhost:123/nameofcontroller

Sample C# Code:

    [Route("/widgets/tools/calc")]

or [Route("widgets/tools/calc")]

Does it matter?

EDIT 1

So after some additional reading it seems that we are using attribute routing... cuz we define the routes in the controller cs file, like this: (please correct me if i'm wrong)

controller1.cs

    [HttpGet]
    [Route("/widgets/{widgetID}/report

controller2.cs

    [HttpGet]
    [Route("widgets/tools/calc

But I'm still trying to understand what the diff is between routes that start with "/" and those that don't.

CodePudding user response:

Read the comments in the code below:

namespace API.Controllers
{
    using Microsoft.AspNetCore.Mvc;

    using System.Collections.Generic;

    /// <summary>
    /// I preffer to use route attributes on controllers ...
    /// ===========================================================================================
    /// By default the mvc pattern looks so: {controller}/{action}   parameters if defined,
    /// ===========================================================================================
    /// </summary>
    [ApiController, Route("/widgets")]
    public class WidgetsController : ControllerBase
    {
        /// <summary>
        /// ... and for specifying additional parameters using of http methods attributes ...
        /// ===========================================================================================
        /// when you use template without leading backslash it is appended to the controller route
        /// and you have GET: /widgets/all instead of just GET: /widgets
        /// ===========================================================================================
        /// [HttpGet]
        /// [Route("all")]
        /// </summary>
        [HttpGet("all")]
        public ActionResult<IEnumerable<object>> Get()
        {
            return this.Ok(new [] { "w1", "w2", "etc." });
        }

        /// <summary>
        /// ... but at the end both of them are valid ...
        /// ===========================================================================================
        /// when you use template with leading backslash the controller route is now OVERWRITTEN
        /// and now looks so: GET: /all/criteria
        /// ===========================================================================================
        /// [HttpGet]
        /// [Route("/all")]
        /// </summary>
        [HttpGet("/all/{filter}")]
        public ActionResult<IEnumerable<object>> Get(string filter)
        {
            return this.Ok(new[] { "w1", "w2" });
        }

        /// <summary>
        /// ===========================================================================================
        /// it is helpfull for defining route parameters like bellow
        /// here the route will looks like GET /widgets/123 
        /// so you can have multiple get methods with different parameters
        /// ===========================================================================================
        /// </summary>
        [HttpGet("{widgetId}")]
        public ActionResult<object> Get(int widgetId)
        {
            return this.Ok(new { widgetId });
        }
    }
}

... whitout specifying the controller route it has no effect on the uri. With specifying the controller route, the uris will look like this:

GET: /widgets/{widgetID}/report
GET: /controller2/widgets/tools/calc
  • Related