Mike Perrenoud
Mike Perrenoud

Reputation: 67898

ApiController Routing Isn't Working

Below I'm going to insert the code for the following:

It is my understand that what I'm doing here is 100% correct, and in fact I've verified that the code lines up with an example I found from Mike Wasson. Hopefully you can show me the way to getting this working!

Thanks all!

Global.asax

Below is the code that exists in the Global.asax and this method is called by Application_Start() which was generated by the template.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

DatabasesController.cs

This is the ApiController, and it inherits from ApiController and was created by the template in Visual Studio. The databases field is declared as Database[] in the class.

public IEnumerable<Database> GetAllDatabases()
{
    return databases;
}

public Database GetDatabaseById(string id)
{
    return databases.Where(d => d.Name == id).FirstOrDefault();
}

Error

This is the error I'm receiving when I try to access it with what should be the default api path (as far as I understand it anyway...HA).

Server Error in '/' Application.
--------------------------------------------------------------------------------

Method not found: 'Void System.Net.Http.Headers.HttpHeaders.AddWithoutValidation(System.String, System.Collections.Generic.IEnumerable`1<System.String>)'. 
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.MissingMethodException: Method not found: 'Void System.Net.Http.Headers.HttpHeaders.AddWithoutValidation(System.String, System.Collections.Generic.IEnumerable`1<System.String>)'.

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: 

[MissingMethodException: Method not found: 'Void System.Net.Http.Headers.HttpHeaders.AddWithoutValidation(System.String, System.Collections.Generic.IEnumerable`1<System.String>)'.]
   System.Web.Http.WebHost.HttpControllerHandler.AddHeaderToHttpRequestMessage(HttpRequestMessage httpRequestMessage, String headerName, String[] headerValues) +0
   System.Web.Http.WebHost.HttpControllerHandler.ConvertRequest(HttpContextBase httpContextBase) +248
   System.Web.Http.WebHost.HttpControllerHandler.BeginProcessRequest(HttpContextBase httpContextBase, AsyncCallback callback, Object state) +79
   System.Web.Http.WebHost.HttpControllerHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +268
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Upvotes: 1

Views: 2299

Answers (1)

JP.
JP.

Reputation: 5606

It looks like you need to upgrade your version of webapi from beta to RC!

Upvotes: 3

Related Questions