0

I have a Web Api project on Visual Studio 2013. suddenly, I started getting this error, without doing any change:

A direct route cannot use the parameter 'controller'. Specify a literal path in place of this parameter to create a route to a controller.

> Line 12:     protected void Application_Start() 
> Line 13:     {
> Line 14:      GlobalConfiguration.Configure(WebApiConfig.Register); 
> Line 15:      GlobalConfiguration.Configuration.IncludeErrorDetailPolicy IncludeErrorDetailPolicy.Always; 
> Line 16:      GlobalConfiguration.Configuration.EnsureInitialized();

When I refresh the page I get another error:

{ "message": "An error has occurred.", "exceptionMessage": "The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.", "exceptionType": "System.InvalidOperationException", "stackTrace": " at System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes()\r\n at System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)\r\n at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)" }

This is my WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {


        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

My Global.asax.cs:

 protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        GlobalConfiguration.Configuration.EnsureInitialized();
    }

And this is my test function on my Test Controller:

  public string Get(int id)
    {
        return "value";
    }

Please advise, any help will be very much appreciated.

user3378165
  • 6,546
  • 17
  • 62
  • 101

2 Answers2

1

My advice would be to comment out the following from your WebApiConfig:

// config.Routes.MapHttpRoute(
//    name: "DefaultApi",
//    routeTemplate: "{controller}/{id}",
//    defaults: new { id = RouteParameter.Optional }
//);

Then ensure that your route attributes are set on your controller as so:

[RoutePrefix("api/test")]
public class TestController : ApiController
{
    [Route("{id}")]
    public string Get(int id)
    {
        return "value";
    };

}

And then place a call to:

http://myapiurl/api/test/1

Starscream1984
  • 3,072
  • 1
  • 19
  • 27
  • 1
    or set the routeTemplate to: `routeTemplate: "api/{controller}/{id}"` – Ric Mar 02 '16 at 16:49
  • There will be quite a few ways to achieve the same thing when it comes to WebAPI routes, but I prefer attribute convention - especially for troubleshooting - being explicit with routes has helped me figure out lots of issues, especially when controllers get big. – Starscream1984 Mar 02 '16 at 16:55
  • I suppose so, on the other hand I find that having attributes everywhere annoys me somewhat... Each to their own. – Ric Mar 02 '16 at 16:56
  • Thank you for your answer, I tried what you suggested but i'm still getting the same error. everything worked perfectly before I got these errors, I don't know why it started erroring... – user3378165 Mar 02 '16 at 17:17
  • Something must have changed to trigger these errors. Googling the message I see it can be caused by a variety of issues, I guess you'll just have to rule all of them out: http://stackoverflow.com/questions/19969228/ensure-that-httpconfiguration-ensureinitialized – Starscream1984 Mar 02 '16 at 17:26
  • You are right, I tried everything I found but nothing helped! – user3378165 Mar 02 '16 at 17:38
  • I ended up re-creating the project.. That solved the issue! – user3378165 Mar 03 '16 at 09:53
  • Hah, typical VS. You should answer your own question with some details of exactly what you did and mark it as correct :) – Starscream1984 Mar 03 '16 at 09:59
1

After searching the Internet for a very long time without finding a solution, I recreated the project from scratch and that solved the issue. It seems to be some kind of bug in Visual Studio 2013.

user3378165
  • 6,546
  • 17
  • 62
  • 101