在Global.asax问题。在MVC4 cs页面

时间:2022-05-04 15:25:40

I my ASP.NET MVC 4 Project, my Global.asax.cs page shows the error on

我我的ASP。NET MVC 4项目,我的Global.asax。cs页面显示错误

 WebApiConfig.Register(GlobalConfiguration.Configuration);

The name 'GlobalConfiguration' does not exist in the current context

“GlobalConfiguration”的名称在当前上下文中不存在

I have done many controllers and Views and all... How can I solve this issue and recover my project?

我做过很多控制器和视图等等。如何解决这个问题并恢复我的项目?

Here is the rest of my code for context

下面是我的上下文代码的其余部分

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

namespace .....
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

6 个解决方案

#1


84  

Make sure you have assembly System.Web.Http.WebHost.dll referenced. This is where GlobalConfiguration is.

确保有assembly System.Web.Http.WebHost。dll的引用。这就是全球配置。

#2


33  

With .NET Framework 4.5.1, GlobalConfiguration is found in System.Web.Http:

在。net Framework 4.5.1中,在System.Web.Http:

PM> Install-Package Microsoft.AspNet.WebApi.WebHost

#3


16  

Update-Package Microsoft.AspNet.WebApi.WebHost -reinstall

更新包Microsoft.AspNet.WebApi。高手写重新安装

This worked for me. I was facing issues with the reference not being updated correctly on Update-package or even removing and installing the package again.

这为我工作。我面临的问题是,在更新包中没有正确更新,甚至是删除和重新安装包。

The above command reinstalls all dependencies, and usually does the trick whenever I face nuget package reference issue.

上面的命令重新安装所有依赖项,每当我遇到nuget包引用问题时,通常都会用到这个技巧。

#4


0  

Also try doing the opposite (no offense) to what David Bohunek suggests. I tried Davids suggestion to no avail. Then I would up removing that reference that was referenced directly in the "Assemblies" section.

也试着做相反的事情(无意冒犯)。我尝试了大卫的建议,但没有成功。然后我将删除“程序集”部分中直接引用的引用。

Then I removed the Web Api 2.2 and Web Api 2.2 Web Host Nugets, performed a clean and then JUST added back the Web Api 2.2 Web Host NuGet.

然后我删除了Web Api 2.2和Web Api 2.2 Web主机NuGet,执行了一次清理,然后只添加了Web Api 2.2 Web主机NuGet。

Try that. Good luck, it worked for me.

试试。祝你好运,我成功了。

#5


0  

in my asp.net webform based on 4.5, adding System.Web.Routing dll and below import in global.aspx resolved the problem

在我的asp.net webform中基于4.5,添加了System.Web。路由dll和以下的全局导入。aspx解决了这一问题

<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.Web.Http" %>

#6


0  

I attempted most of the fixes listed above to no avail. I am sure 9 out of 10 are normally resolved by the posts above but for this error, but none of these helped me.

我尝试了上面列出的大多数修复,但没有任何效果。我确信10个中的9个通常是由上面的帖子来解决的,但是对于这个错误,这些都没有帮助我。

My fix for this error, was that I was renaming my namespace and had two different references to the namespace which in the end caused my issue. Since I wanted to update the namespace with something other than I started with, I ended up fixing this issue by changing my namespace to the newer one in my WebApiConfig.cs. Apparently it was one of the last files which I hadn't used the newer namespace name.

我对这个错误的修正是,我重新命名了我的名称空间,并且有两个不同的名称空间引用,这最终导致了我的问题。因为我想用其他东西更新名称空间,所以我最终通过将名称空间更改为WebApiConfig.cs中的新名称空间来解决这个问题。显然,它是我没有使用更新的名称空间名称的最后文件之一。

Well, once I changed that then my errors went away. Also, another fix for this same issue was to specify using old namespace name; which also fixed it temporarily which got me to just rename it in the WebApiConfig.cs with the new name.

一旦我改变了它,我的错误就消失了。另外,同样的问题的另一个解决方案是使用旧的名称空间名称;它也临时修复了它,这让我在WebApiConfig中重命名了它。有了新名字的cs。

#1


84  

Make sure you have assembly System.Web.Http.WebHost.dll referenced. This is where GlobalConfiguration is.

确保有assembly System.Web.Http.WebHost。dll的引用。这就是全球配置。

#2


33  

With .NET Framework 4.5.1, GlobalConfiguration is found in System.Web.Http:

在。net Framework 4.5.1中,在System.Web.Http:

PM> Install-Package Microsoft.AspNet.WebApi.WebHost

#3


16  

Update-Package Microsoft.AspNet.WebApi.WebHost -reinstall

更新包Microsoft.AspNet.WebApi。高手写重新安装

This worked for me. I was facing issues with the reference not being updated correctly on Update-package or even removing and installing the package again.

这为我工作。我面临的问题是,在更新包中没有正确更新,甚至是删除和重新安装包。

The above command reinstalls all dependencies, and usually does the trick whenever I face nuget package reference issue.

上面的命令重新安装所有依赖项,每当我遇到nuget包引用问题时,通常都会用到这个技巧。

#4


0  

Also try doing the opposite (no offense) to what David Bohunek suggests. I tried Davids suggestion to no avail. Then I would up removing that reference that was referenced directly in the "Assemblies" section.

也试着做相反的事情(无意冒犯)。我尝试了大卫的建议,但没有成功。然后我将删除“程序集”部分中直接引用的引用。

Then I removed the Web Api 2.2 and Web Api 2.2 Web Host Nugets, performed a clean and then JUST added back the Web Api 2.2 Web Host NuGet.

然后我删除了Web Api 2.2和Web Api 2.2 Web主机NuGet,执行了一次清理,然后只添加了Web Api 2.2 Web主机NuGet。

Try that. Good luck, it worked for me.

试试。祝你好运,我成功了。

#5


0  

in my asp.net webform based on 4.5, adding System.Web.Routing dll and below import in global.aspx resolved the problem

在我的asp.net webform中基于4.5,添加了System.Web。路由dll和以下的全局导入。aspx解决了这一问题

<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.Web.Http" %>

#6


0  

I attempted most of the fixes listed above to no avail. I am sure 9 out of 10 are normally resolved by the posts above but for this error, but none of these helped me.

我尝试了上面列出的大多数修复,但没有任何效果。我确信10个中的9个通常是由上面的帖子来解决的,但是对于这个错误,这些都没有帮助我。

My fix for this error, was that I was renaming my namespace and had two different references to the namespace which in the end caused my issue. Since I wanted to update the namespace with something other than I started with, I ended up fixing this issue by changing my namespace to the newer one in my WebApiConfig.cs. Apparently it was one of the last files which I hadn't used the newer namespace name.

我对这个错误的修正是,我重新命名了我的名称空间,并且有两个不同的名称空间引用,这最终导致了我的问题。因为我想用其他东西更新名称空间,所以我最终通过将名称空间更改为WebApiConfig.cs中的新名称空间来解决这个问题。显然,它是我没有使用更新的名称空间名称的最后文件之一。

Well, once I changed that then my errors went away. Also, another fix for this same issue was to specify using old namespace name; which also fixed it temporarily which got me to just rename it in the WebApiConfig.cs with the new name.

一旦我改变了它,我的错误就消失了。另外,同样的问题的另一个解决方案是使用旧的名称空间名称;它也临时修复了它,这让我在WebApiConfig中重命名了它。有了新名字的cs。