路径模板分隔符'/'不能连续出现 - 属性路由问题

时间:2015-05-10 14:54:16

标签: c# asp.net .net asp.net-web-api asp.net-web-api2

配置与错误无关

这是我在App_Start / WebApiConfig.cs中对Web API的配置:

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 }
        );.....

这是我的global.asax类:

GlobalConfiguration.Configure(WebApiConfig.Register);

这是错误

但是每当应用程序启动时,我都会遇到此异常:

  

路径模板分隔符'/'不能连续出现。它必须用参数或文字值

分隔

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

错误是由Route属性

引起的

这就是我在控制器上使用属性路由的方式:

[RoutePrefix("v1")]
public class MarketController : ApiController
{
    [HttpGet]
    [Route("/marketdata/tickerinfo")]
    public IHttpActionResult TickerInfo(string currencyPair)
    {

4 个答案:

答案 0 :(得分:50)

出现上述错误的原因是我在操作的路由属性中使用了额外的“/”。上述控制器中定义的操作应如下所示:

    [HttpGet]
    [Route("marketdata/tickerinfo")]
    public IHttpActionResult TickerInfo(string currencyPair)
    {

答案 1 :(得分:4)

我用两个(或更多)斜杠得到了同样的错误。

[Route("marketdata//tickerinfo")]

更改字符串时很容易发生。

答案 2 :(得分:0)

我收到了同样的错误,但它是由我案例中的ActionName属性引起的。

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

虽然这打破了帮助页面,但我仍然能够到达端点api/controller?code=123。当我删除ActionName属性时,错误消失了。

答案 3 :(得分:0)

我在一个动作上结合了[RoutePrefix]和[Route]来收到此问题。

./score.py 
./myscripts/train.py 
./myscripts/common.py

关于我执行的操作

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

当我更改时:[HttpPost, Route("{someVariable:int}"] [RoutePrefix("/api/my/application")],一切都很好。