How to distinguish between a query string variable and a URL segment variable with Web API 2.0 attribute routing?

220 Views Asked by At

Lets say I have the following ApiController and method:

[RoutePrefix("configs")]
public class FullConfigController : ApiController
{
    [Route("{ID}")]
    [HttpGet]
    public string GetConfig(Guid ID)
    {
        return "my config data";
    }
}

It's a simple GET method designed to return some basic configuration data based on that configurations ID which is represented by a GUID. My intention is to call the end point as such

http://<url>/configs/70C55EDA-9671-4D75-A715-0835DB8AC2E3

However this does not route through the engine. Yet this

http://<url>/configs?ID=70C55EDA-9671-4D75-A715-0835DB8AC2E3

does. How do I specify that I do not want a query string parameter here but a URL segment parameter instead?

EDIT: Request for the WebApiConfig

public class RouteTemplateVariables
{
    public const string PluginRoot = "PluginRoot";
    public const string CorePluginRoot = "CorePluginRoot";
    public const string AdditionalSegments = "AdditionalSegments";
}

public static class WebApiConfig
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public static void Register(HttpConfiguration config)
    {
        log.Info("WebApiConfig.Register()");

        // Attribute routing
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "PluginApi",
            routeTemplate: "api/plugin/{" + RouteTemplateVariables.PluginRoot + "}/{*" + RouteTemplateVariables.AdditionalSegments + "}",
            defaults: new { AdditionalSegments = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "CoreApi",
            routeTemplate: "api/{" + RouteTemplateVariables.CorePluginRoot + "}/{*" + RouteTemplateVariables.AdditionalSegments + "}",
            defaults: new { AdditionalSegments = RouteParameter.Optional }
        );
    }
}
0

There are 0 best solutions below