下面我将插入以下代码:
- 全球.asax
- 数据库控制器 ( ApiController )
- 尝试导航到默认 api 路由时收到的错误。
据我了解,我在这里所做的事情是 100% 正确的,事实上我已经验证了代码与我从Mike Wasson找到的示例一致。希望你能告诉我如何让这个工作!
谢谢大家!
全球.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 }
);
}
数据库控制器.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