Вопрос

I want to do a routing for an API, that is versioned.

The Folder structure is like this

/api/v1-0/ApiController.cs
/api/v1-1/ApiController.cs
/api/v1-2/ApiController.cs

So I added the route

    routes.MapRoute(
        name: "DefaultApiVersioned",
        url: "api/v{*Version}/{controller}/{action}/{id}",
        defaults: new { controller = "Api", action = "Index", id = UrlParameter.Optional }
    );

But when the route is added, I get an "ArgumentException". The exception message is:

"Ein Pfadsegment, das mehrere Abschnitte enthält, z.B. einen Literalabschnitt oder einen Parameter, darf keinen Catchall-Parameter enthalten. "

Translation:

"A path segment, that has multiple sections, such as a literal section or a parameter, can not have a catchall parameter"

So how do I have to change my route config?

Это было полезно?

Решение

You don't need a wildcard:

routes.MapRoute(
    name: "DefaultApiVersioned",
    url: "api/v{version}/{controller}/{action}/{id}",
    defaults: new { controller = "Api", action = "Index", id = UrlParameter.Optional }
);

The wildcard can only be used as last segment of a route definition.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top