路由模板分隔符'/'不能连续出现-属性路由问题
The route template separator character '/' cannot appear consecutively - Attribute routing issue
配置与错误无关
这是我在 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 class:
GlobalConfiguration.Configure(WebApiConfig.Register);
这是错误
但是每当应用程序启动时,我都会收到此异常:
The route template separator character '/' cannot appear consecutively. It must be separated by either a parameter or a literal value
堆栈跟踪:
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
错误是由路由属性引起的
这就是我在控制器上使用属性路由的方式:
[RoutePrefix("v1")]
public class MarketController : ApiController
{
[HttpGet]
[Route("/marketdata/tickerinfo")]
public IHttpActionResult TickerInfo(string currencyPair)
{
出现上述错误的原因是我在动作的路由属性中使用了额外的'/'。上面controller中定义的action应该是这样的:
[HttpGet]
[Route("marketdata/tickerinfo")]
public IHttpActionResult TickerInfo(string currencyPair)
{
我有两个(或更多)斜线的相同错误。
[Route("marketdata//tickerinfo")]
换字符串的时候很容易出现
我遇到了同样的错误,但在我的案例中这是由 ActionName
属性引起的。
[ActionName("")]
public void Get(string code)
虽然这破坏了帮助页面,但我仍然能够到达端点 api/controller?code=123
。当我删除 ActionName
属性时,错误消失了。
我收到的这个问题是关于一个动作的 [RoutePrefix] 和 [Route] 的组合。
[RoutePrefix("/api/my/application")]
在 行动 我有
[HttpPost, Route("{someVariable:int}"]
当我改变:[RoutePrefix("/api/my/application")]
到[RoutePrefix("api/my/application")]
一切都很好。
配置与错误无关
这是我在 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 class:
GlobalConfiguration.Configure(WebApiConfig.Register);
这是错误
但是每当应用程序启动时,我都会收到此异常:
The route template separator character '/' cannot appear consecutively. It must be separated by either a parameter or a literal value
堆栈跟踪:
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
错误是由路由属性引起的
这就是我在控制器上使用属性路由的方式:
[RoutePrefix("v1")]
public class MarketController : ApiController
{
[HttpGet]
[Route("/marketdata/tickerinfo")]
public IHttpActionResult TickerInfo(string currencyPair)
{
出现上述错误的原因是我在动作的路由属性中使用了额外的'/'。上面controller中定义的action应该是这样的:
[HttpGet]
[Route("marketdata/tickerinfo")]
public IHttpActionResult TickerInfo(string currencyPair)
{
我有两个(或更多)斜线的相同错误。
[Route("marketdata//tickerinfo")]
换字符串的时候很容易出现
我遇到了同样的错误,但在我的案例中这是由 ActionName
属性引起的。
[ActionName("")]
public void Get(string code)
虽然这破坏了帮助页面,但我仍然能够到达端点 api/controller?code=123
。当我删除 ActionName
属性时,错误消失了。
我收到的这个问题是关于一个动作的 [RoutePrefix] 和 [Route] 的组合。
[RoutePrefix("/api/my/application")]
在 行动 我有
[HttpPost, Route("{someVariable:int}"]
当我改变:[RoutePrefix("/api/my/application")]
到[RoutePrefix("api/my/application")]
一切都很好。