Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
smaglio81 opened this issue Aug 3, 2017 · 2 comments

Comments

@smaglio81
Copy link

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

@commonsensesoftware
Copy link
Collaborator

#73 was more about providing a default value for the API version in the middle of the template. That isn't something supported by the platform I'm afraid. You'd have to create multiple routes declaratively with attributes or imperatively at runtime (as described in #73). I actually did investigate using default values in the template like /v{version:apiVersion=42}, but it doesn't work. :(

I'm not sure that's necessarily your issue. I find it odd that /v2 works, but /v1 does not. They should either both work or both fail. Can you share your repro? You can just zip it up and attach it to this thread. Thanks.

@smaglio81
Copy link
Author

Thanks for the quick response!

Code that reproduces the problem can be found here: https://github.com/smaglio81/WebApiVersioningV1Test

When prepping the files, I made a small change and the problem disappeared:

  • If I move the Controllers folder out of the subfolder /V1, and into the root of the site.
  • And, I delete the /V1 folder ...

Then the problem doesn't occur. It only occurs if the /V1 folder exists.

Ohhhhh ... I remember reading that the default behavior of Web API routing is something like "if a file or folder actually exists on disk, then Web API routing will not process the request." So, the 403 Forbidden is because the permissions to list a folders contents are denied.

Thanks for being there and letting me bounce the issue off you; I think I'll mark this as closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants