asp.net-mvc HttpConfiguration 缺少程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16795167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:52:57  来源:igfitidea点击:

HttpConfiguration missing assembly

asp.net-mvcasp.net-mvc-4

提问by Soph

I am building a site using MVC4. I couldn't even begin that I am having a strange error. This is my code:

我正在使用 MVC4 构建一个站点。我什至无法开始说我有一个奇怪的错误。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace MyProject
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

The error message: The type or name of the assembly 'HttpConfiguration' does not exist in the namespace 'System.Web.Http' (are you missing a using directive or an assembly reference?)

错误消息:命名空间“System.Web.Http”中不存在程序集“HttpConfiguration”的类型或名称(您是否缺少 using 指令或程序集引用?)

It also doesn't recognize the readonly field RouteParameter. It occurs to me that it may have something to do with the version, my System.Web dll is version 4.0.0.0.

它也无法识别只读字段 RouteParameter。我突然想到它可能与版本有关,我的 System.Web dll 是版本 4.0.0.0。

Another additional piece of information is that when I type "using System.Web. ", Intellisense DOES recognize the .Http dll

另一个附加信息是,当我输入“使用 System.Web。”时,Intellisense 会识别 .Http dll

HOWEVER, in the code, it does not recognize any of its parameters.

但是,在代码中,它无法识别其任何参数。

采纳答案by Soph

I solved it. The issue was that, when I created the site, I updated all packages via Nuget. However, as I wasn′t going to use Entity Framework, I uninstalled it. In order to be able to uninstall that package, it required for me to uninstall Microsoft.AspNet.Providers.Core 1.2 as well... and I did.

我解决了。问题是,当我创建站点时,我通过 Nuget 更新了所有包。然而,由于我不打算使用实体框架,我卸载了它。为了能够卸载该软件包,我还需要卸载 Microsoft.AspNet.Providers.Core 1.2 ......我做到了。

This missing package messed things up. I cleared the project and started all over again. I could have also used the Update-Package command in the PM Console and would have restored all lost packages. However, since I had done so much mess compared to the little (next to null) work I had done, I decided to start it all over again.

这个丢失的包把事情搞砸了。我清除了项目并重新开始。我也可以在 PM 控制台中使用 Update-Package 命令并恢复所有丢失的包。然而,由于与我所做的小(在 null 旁边)工作相比,我做了这么多的混乱,我决定重新开始。

Thanks anyway!

不管怎么说,还是要谢谢你!

回答by mm201

I encountered this error when cloning an MVC 4.0 application which previously worked for me. My problem was that the Microsoft.Net.HttpNuGet package wasn't restoring properly. I solved it from the NuGet console:

我在克隆以前对我有用的 MVC 4.0 应用程序时遇到了这个错误。我的问题是Microsoft.Net.HttpNuGet 包没有正确恢复。我从 NuGet 控制台解决了它:

Update-Package -reinstall Microsoft.Net.Http

Update-Package -reinstall Microsoft.Net.Http

回答by David Martin

For others facing this issue, my solution was different. Firstly I was downgrading a project from .net 4.5 to .net 4.0, but the symptoms were the same as above:

对于其他面临这个问题的人,我的解决方案是不同的。首先,我将一个项目从 .net 4.5 降级到 .net 4.0,但症状与上述相同:

The type or name of the assembly 'HttpConfiguration' does not exist in the namespace 'System.Web.Http' (are you missing a using directive or an assembly reference?)

命名空间“System.Web.Http”中不存在程序集“HttpConfiguration”的类型或名称(您是否缺少 using 指令或程序集引用?)

I stumbled across a blog post which explained that there is a link between Newtonsoft.Jsonand HttpConfiguration, I removed the reference to Newtonsoft.Jsonfrom the project and the packages.configfile then re-installed Newtonsoft.Jsonfrom the package manager console:

我偶然发现了一篇博客文章,其中解释了Newtonsoft.Json和之间的链接HttpConfiguration,我Newtonsoft.Json从项目中删除了对 的引用,packages.config然后Newtonsoft.Json从包管理器控制台重新安装了文件:

Install-Package Newtonsoft.Json

I was not able to uninstall Newtonsoft.Jsonfrom the package manage console due to other dependencies, hence the need to manually remove the reference.

Newtonsoft.Json由于其他依赖项,我无法从包管理控制台卸载,因此需要手动删除引用。

Here's the link to the article: Newtonsoft.Json Hidden Dependencny on HttpConfiguration Breaks Compilation

这是文章的链接:Newtonsoft.Json Hidden Dependencny on HttpConfiguration Breaks Compilation

回答by Prerna Jain

1.In Solution Explorer, right-click the project node and click Add Reference.

1.在解决方案资源管理器中,右键单击项目节点,然后单击添加引用。

2.In the Add Reference dialog box, select Assemblies tab. And type the “system.web.http” in the search box in the upper right corner.

2.在“添加引用”对话框中,选择“程序集”选项卡。然后在右上角的搜索框中输入“system.web.http”。

3.Select the components you want to reference, if it is already selected than first remove the reference than **again Add Reference.

3.选择要引用的组件,如果已经被选中,先删除引用,然后**再添加引用。

Rebuild the project again. Please let me know if it works. Thank you.

再次重建项目。请让我知道它是否有效。谢谢你。

回答by rock_walker

when you updated and reinstalled all the AspNet packages from Nuget, and seems there were no appropriate solution - restart of Visual Studio saved me.

当您从 Nuget 更新并重新安装所有 AspNet 包时,似乎没有合适的解决方案 - 重新启动 Visual Studio 救了我。

回答by Carl Heinrich Hancke

We started experiencing this issue on one of our projects since last week. The project was building fine 2 weeks ago & then all of a sudden started throwing out this exception. No changes were made in in the nuget packages for this project. Even checking out an older revision from SVN didn't result in a successful build.

自上周以来,我们开始在我们的一个项目中遇到此问题。该项目在 2 周前构建良好,然后突然开始抛出此异常。此项目的 nuget 包中未进行任何更改。即使从 SVN 检出旧版本也没有成功构建。

Out of desperation, we upgraded the solution from VS2012 to VS2013 & lo and behold, it built! We suspect, but were unable to confirm, that a tooling update done in Visual Studio 2013 had affected this library in Visual Studio 2012.

无奈之下,我们将解决方案从 VS2012 升级到 VS2013 & 瞧,它构建了!我们怀疑,但无法确认,在 Visual Studio 2013 中完成的工具更新影响了 Visual Studio 2012 中的这个库。

This may not work for all, but there is an option to upgrade the solution, this seems to resolve the problem.

这可能并不适用于所有人,但可以选择升级解决方案,这似乎可以解决问题。

回答by Calle Bjernekull

Make sure you compile for Framework 4.5 and not 4.0. That solved the problem for me

确保针对 Framework 4.5 而不是 4.0 进行编译。这为我解决了问题

回答by Rob Von Nesselrode

Yet another scenario. I accept that this is only loosely related to the question as I'm purely Web API. However mysearch led me here so...

又一个场景。我接受这只是与问题的松散相关,因为我纯粹是 Web API。然而,我的搜索将我带到了这里,所以......

I had an existing project in the solution that targeted Framework 4.5.2.

我在针对 Framework 4.5.2 的解决方案中有一个现有项目。

When I added a new project, it went in as 4.5 as I wasn't paying enough attention. HttpConfiguration duly vanished from the Web API Register method.

当我添加一个新项目时,由于我没有给予足够的关注,它的得分为 4.5。HttpConfiguration 正式从 Web API 注册方法中消失了。

Changing the target to 4.5.2 resolved the HttpConfiguration issue. Mind you it also caused some other grief with RouteParameter: now I had two definitions, one in System.Web.Http and the other in System.Web.UI.WebControls. Easy enough I guess.

将目标更改为 4.5.2 解决了 HttpConfiguration 问题。请注意,它也给 RouteParameter 带来了其他一些麻烦:现在我有两个定义,一个在 System.Web.Http 中,另一个在 System.Web.UI.WebControls 中。我想很容易。

My feeling is that there is more to this...

我的感觉是这还有更多……