ApiController路由不起作用

时间:2012-06-26 00:14:14

标签: asp.net-mvc-3 asp.net-mvc-routing asp.net-web-api

下面我将插入以下代码:

  • Global.asax中
  • DatabasesController( ApiController
  • 尝试导航到默认api路径时收到的错误。

据我所知,我在这里所做的是100%正确,事实上我已经验证了代码与我在Mike Wasson找到的示例对齐。 希望您能告诉我如何使其正常工作!

全部谢谢!

Global.asax中

以下是Global.asax中存在的代码,此方法由Application_Start()调用,该方法由模板生成。

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

这是ApiController,它继承自ApiController,由Visual Studio中的模板创建。 databases字段在类中声明为Database[]

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

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

错误

当我尝试使用默认的api路径访问它时,这是我收到的错误(据我所知,无论如何...... 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

1 个答案:

答案 0 :(得分:3)

看起来你需要将你的web api版本从beta升级到RC!