42

The configuration has nothing to do with the error

This is my configuration for the Web API in App_Start/WebApiConfig.cs:

public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );.....

And this is my global.asax class:

GlobalConfiguration.Configure(WebApiConfig.Register);

This is the error

But whenever the application is starting, I get this exception:

The route template separator character '/' cannot appear consecutively. It must be separated by either a parameter or a literal value

StackTrace:

at System.Web.Http.Routing.RouteParser.Parse(String routeTemplate)
at System.Web.Http.Routing.DirectRouteFactoryContext.CreateBuilder(String template, IInlineConstraintResolver constraintResolver)
at System.Web.Http.Routing.DirectRouteFactoryContext.CreateBuilderInternal(String template)
at System.Web.Http.Routing.DirectRouteFactoryContext.CreateBuilder(String template)
at System.Web.Http.RouteAttribute.System.Web.Http.Routing.IDirectRouteFactory.CreateRoute(DirectRouteFactoryContext context)
at System.Web.Http.Routing.AttributeRoutingMapper.CreateRouteEntry(String prefix, IDirectRouteFactory factory, IReadOnlyCollection`1 actions, IInlineConstraintResolver constraintResolver, Boolean targetIsAction)
at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, String prefix, IReadOnlyCollection`1 factories, IReadOnlyCollection`1 actions, IInlineConstraintResolver constraintResolver, Boolean targetIsAction)
at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpControllerDescriptor controller, IInlineConstraintResolver constraintResolver)
at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver)
at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<>c__DisplayClass4.<MapAttributeRoutes>b__1()
at System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer)
at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<MapAttributeRoutes>b__0(HttpConfiguration config)
at System.Web.Http.HttpConfiguration.EnsureInitialized()
at System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback)
at Sample.Rest.WebHost.WebApiApplication.Application_Start() in d:\sample\Sample.Rest.WebHost\Global.asax.cs:line 33

The error is caused by the Route attribute

This is how I am using attribute routing on my controller:

[RoutePrefix("v1")]
public class MarketController : ApiController
{
    [HttpGet]
    [Route("/marketdata/tickerinfo")]
    public IHttpActionResult TickerInfo(string currencyPair)
    {
JotaBe
  • 38,030
  • 8
  • 98
  • 117
Syed Waqas
  • 2,576
  • 4
  • 29
  • 36

5 Answers5

65

The reason for the above error is that I am using an additional '/' in the route attribute for the action. The action defined in the above controller should be as follows:

    [HttpGet]
    [Route("marketdata/tickerinfo")]
    public IHttpActionResult TickerInfo(string currencyPair)
    {
Syed Waqas
  • 2,576
  • 4
  • 29
  • 36
  • It's worth noting you can get this error from OTHER routes in the same controller... This had me baffled for a while. – Efreeto Aug 26 '20 at 21:59
9

I've got the same error with two (or more) slashes.

[Route("marketdata//tickerinfo")]

It's easy to happen when you change the string.

Setar
  • 261
  • 3
  • 10
1

I got the same error however it was caused by the ActionName attribute in my case.

[ActionName("")]
public void Get(string code)

Although this broke the Help pages I was still able to reach the endpoint api/controller?code=123. When I removed the ActionName attribute the error disappeared.

fireydude
  • 1,181
  • 10
  • 23
1

I was receiving this issue with a combination of a [RoutePrefix] and a [Route] on an action.

[RoutePrefix("/api/my/application")]

on the action I had

[HttpPost, Route("{someVariable:int}"]

When I changed: [RoutePrefix("/api/my/application")]to[RoutePrefix("api/my/application")] everything was fine.

Zach Wymer
  • 540
  • 9
  • 11
0

The error was generated due to duplicate entries under the same [Route("YOUR_FUNCTION_ENTRY_POINT)] in different controllers

Make sure you dont have duplicate entries in you MVC API.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32002622) – Lajos Arpad Jun 14 '22 at 23:16