Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

c# - The route template separator character '/' cannot appear consecutively - Attribute routing issue

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:sampleSample.Rest.WebHostGlobal.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)
    {
question from:https://stackoverflow.com/questions/30152847/the-route-template-separator-character-cannot-appear-consecutively-attribu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
    {

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...