1

I have a Web API 2 project hosted through an OWIN middleware. Everything worked perfectly fine and I am able to call my APIs as expected. But, my WebApiConfig defines the default route as follows:

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

Accordingly, I have to call my APIs using URLs similar to: /api/values/dosomething

This worked for me until I decided to document my API. For that, I first tried using the WebAPI Help Page package which did not work. Then I thought I should try Swashbuckle Swagger and see if that helps me avoid the problem altogether, but unfortunately, in both cases I got the same error:

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

After trying a few things, it turned out that when change the route template and remove the {action} part, the error is gone. But, I cannot really do that because the whole project assumes that URLs include the action method name in them.

So anyway, I would like to know the following:

  1. Why is this happening in the first place?
  2. Is there a way to modify this behavior?

Thanks in advance!

Kassem
  • 8,116
  • 17
  • 75
  • 116
  • this is a duplicate of https://stackoverflow.com/questions/30152847/the-route-template-separator-character-cannot-appear-consecutively-attribu – fireydude Jun 14 '18 at 09:46

1 Answers1

6

I fetch the same problem. Below is my working code.

[RoutePrefix("api/User")]
public class UserController : ApiController
{
    [Route("login")]
    public IHttpActionResult Get()
    {       }
}

when I change Route before methods from

[Route("login")]
public IHttpActionResult Get()
{       }

to

[Route("/login")]
public IHttpActionResult Get()
{       }

I got same error. It will generate if you add an extra / before any methods of your controller(if it is not called at first time).

I am not sure about your condition. I share my situation if anyone gets help.

reza.cse08
  • 5,938
  • 48
  • 39