Skip to content

Routes starting with v{apiVersion} and controller predefined will not respond to /v1 #170

Closed
@smaglio81

Description

@smaglio81

Hello,

This is a very specific use case:

  • The WebApi Route doesn't use the "api/" prefix
  • The WebApi Route defines the controller
  • Using Url Path Segment Routing with ApiVersionRouteConstraint

With this setup:

  • /v1 results in a 403 Forbidden error
  • /v1.0 works
  • /v2 works
  • /v2.0 works

And, it's only the /v1 route. If you change the DefaultApiVersion or other configuration options, the problem continues to occur only on /v1.

To reproduce the issue, I used:

  • VS 2015
  • A .NET Framework 4.6.1 ASP.NET Web Application (Empty with WebAPI checked)
  • Install-Package Microsoft.AspNet.WebApi.Versioning
  • Updated WebApiConfig.cs
  • Created \Controllers\EchoController.cs

WebApiConfig.cs:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

		// Setup API Versioning
		config.AddApiVersioning(o =>
		{
			// commenting this out still produces the /v1 403 error
			o.DefaultApiVersion = new ApiVersion(2,0);
		});

		config.Routes.MapHttpRoute(
			name: "DefaultApi-Versioned",
			routeTemplate: "v{apiversion}",
			defaults: new { controller = "Echo" },
			constraints: new { apiVersion = new ApiVersionRouteConstraint() }
		);
	}
}

EchoController.cs:

[ApiVersion("1")]
public class EchoController : ApiController
{

	public Agreement Get()
	{
		return new Agreement(GetType().FullName, "version-1", Request.GetRequestedApiVersion().ToString());
	}
}

public class Agreement
{
	public Agreement(string controller, string accountId, string apiVersion)
	{
		Controller = controller;
		AccountId = accountId;
		ApiVersion = apiVersion;
	}

	public string Controller { get; set; }

	public string AccountId { get; set; }

	public string ApiVersion { get; set; }
}
}

namespace WebApiVersioningV1Test.V2.Controllers
{
using WebApiVersioningV1Test.V1.Controllers;

[ApiVersion("2")]
public class EchoController : ApiController
{

	public Agreement Get()
	{
		return new Agreement(GetType().FullName, "version-2", Request.GetRequestedApiVersion().ToString());
	}
}

I've read through the Known Limitations with Url Path Segment Routing, and I've read through Issue 73 (BTW- The link on the wiki page needs to be updated). But, it doesn't seem to describe my exact issue.

Do you have any thoughts on what might be going on?

Thanks In Advance

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions