【问题标题】:WebApi routing dependency using ASP.Net visual studio 2010使用 ASP.Net Visual Studio 2010 的 WebApi 路由依赖
【发布时间】:2013-05-10 09:42:33
【问题描述】:

我正在 Visual Studio 2010 上创建一个 WepApi,我使用 NuGet 作为 api。现在,当我在全局文件中注册 API 时,我收到以下错误。下面的代码在 api 的 application_Start 中找到。

protected void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            RouteTable.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = System.Web.Http.RouteParameter.Optional }
            );
        }

$RouteTable.Routes.MapHttpRoute 出现以下错误 - 实例参数:

cannot convert from 'System.Web.Routing.RouteCollection' to 'System.Web.Http.HttpRouteCollection'

有什么建议为什么会出现这个错误?

【问题讨论】:

    标签: c# visual-studio-2010 asp.net-web-api nuget-package maproute


    【解决方案1】:

    你需要使用

    GlobalConfiguration.Configuration.Routes.MapHttpRoute
    

    而不是

    RouteTable.Routes.MapHttpRoute
    

    【讨论】:

    • 在全局文件中找不到 GlobalConfiguration
    • GlobalConfiguration 是 System.Web.Http 命名空间的一部分。您应该在 Global.asax 文件中对此进行引用。
    【解决方案2】:

    不知道是否适用于VS 2010,但在全局中添加以下参考

    using System.Net.Http;
    using System.Web.Http;
    using System.Web.Http.WebHost;
    using System.Web.Routing;
    

    【讨论】:

    • DoNo 是什么意思?你的意思是“不知道”吗?
    • 那么请编辑您的问题以说明这一点。请在 StackOverflow 上使用英文,不要使用无意义的缩写。
    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 2011-12-28
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2017-08-05
    相关资源
    最近更新 更多