0

I am using the Attribute Routing for Web API nuget package.

My RegisterRoutes(HttpCollection routes) method looks like this:

public static void RegisterRoutes(HttpRouteCollection routes) 
{
    routes.MapHttpAttributeRoutes(config =>
    {
        config.AddRoutesFromAssembly(Assembly.GetExecutingAssembly());
        config.AutoGenerateRouteNames = true;
        config.InMemory = true;
        config.PreserveCaseForUrlParameters = true;

        // Define type constraints for parameters on GETs
        config.AddDefaultRouteConstraint("id", new IntRouteConstraint());
        config.AddDefaultRouteConstraint("studentId", new IntRouteConstraint());
        config.AddDefaultRouteConstraint("campusId", new IntRouteConstraint());
    });
}

Here is one of my controllers (comments removed for brevity):

[AttributeRouting.RoutePrefix("SchoolInfo/Campus")]
public class CampusController : ApiControllerBase
{
[ResponseType(typeof(CampusApiEntity))]
[GET("{id}")]
public HttpResponseMessage Get(int id)
    {
        // Do stuff here - not important for the question
    }
}

The issue I am having is that I can access the above method like this: http://localhost:55697/schoolinfo/campus?id=2

but when I try to do this: http://localhost:55697/schoolinfo/campus/2

I get the following error:

The constraint entry 'id' on the route with route template 'SchoolInfo/Campus/{id}' must have a string value or be of a type which implements 'IHttpRouteConstraint'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The constraint entry 'id' on the route with route template 'SchoolInfo/Campus/{id}' must have a string value or be of a type which implements 'IHttpRouteConstraint'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: The constraint entry 'id' on the route with route template 'SchoolInfo/Campus/{id}' must have a string value or be of a type which implements 'IHttpRouteConstraint'.]
System.Web.Http.Routing.HttpRoute.ProcessConstraint(HttpRequestMessage request, Object constraint, String parameterName, HttpRouteValueDictionary values, HttpRouteDirection routeDirection) +293 System.Web.Http.Routing.HttpRoute.ProcessConstraints(HttpRequestMessage request, HttpRouteValueDictionary values, HttpRouteDirection routeDirection) +99
System.Web.Http.Routing.HttpRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) +195
AttributeRouting.Web.Http.Framework.HttpAttributeRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) +131
System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext) +107
System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext) +233
System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +60
System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +82
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044

I'm not sure how to go about solving this because I already have a constraint specified for the id parameter. When I googled the error, the only results I found involve setting config settings that I already have above.

I checked my routes.axd file and I do have this in there:

GET, HEAD, OPTIONS  SchoolInfo/Campus/{id}  controller: Campus, action: Get, id: IntRouteConstraint, namespaces: [MyAssemblyName].Controllers.SchoolInfo, defaultSubdomain: www, routeName: Campus_Get_1

Any ideas?

Update:

If I change my GET attribute from [GET("{id}")] to [GET("{id:int}")], it works. So the issue seems to be with the default route constraints. The problem is I was really hoping to use those because I have id all over the place and it seems redundant to constantly be specifying its type when it's always an int.

Another thing I noticed is that the IntRouteConstraint class is found in 4 different assemblies:

AttributeRouting.Web.Constraints
AttributeRouting.Web.Http.Constraints
System.Web.Http.Routing.Constraints
System.Web.Mvc.Routing.Constraints

However, the AddDefaultRouteConstraint() method takes an IRouteConstraint as its second parameter, and only the IntRouteConstraint in AttributeRouting.Web.Constraints implements that interface, so that is the assembly I included. I'm not sure if that is the correct one or not. Maybe that is somehow related to the issue?

Any help would be much appreciated. Thanks!

mayabelle
  • 9,804
  • 9
  • 36
  • 59
  • 1
    Web API has in built attribute routing support..have you considered using it? – Kiran Oct 26 '13 at 15:44
  • 1
    Thank you!! I think I thought I had to use the library, and it must have caused a conflict with the built in functionality. I got it working after uninstalling the library. – mayabelle Oct 28 '13 at 14:44

0 Answers0