以下に、次のコードを挿入します。
- Global.asax
- DatabasesController(ApiController)
- デフォルトのAPIルートに移動しようとしたときに受け取るエラー。
私がここで行っていることは100%正しいことを理解しています。実際、コードがMikeWassonから見つけた例と一致していることを確認しました。うまくいけば、これを機能させる方法を教えてください!
皆さんありがとう!
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
であり、Visual Studioのテンプレートから継承しApiController
、テンプレートによって作成されました。databases
フィールドはクラスのように宣言されDatabase[]
ます。
public IEnumerable<Database> GetAllDatabases()
{
return databases;
}
public Database GetDatabaseById(string id)
{
return databases.Where(d => d.Name == id).FirstOrDefault();
}
エラー
これは、デフォルトのAPIパスであるはずの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