I my ASP.NET MVC 4 Project, my Global.asax.cs page shows the error on
WebApiConfig.Register(GlobalConfiguration.Configuration);
The name 'GlobalConfiguration' does not exist in the current context
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);
}
}
}
Make sure you have assembly System.Web.Http.WebHost.dll referenced. This is where GlobalConfiguration is.
With .NET Framework 4.5.1, GlobalConfiguration is found in System.Web.Http:
PM> Install-Package Microsoft.AspNet.WebApi.WebHost
Update-Package Microsoft.AspNet.WebApi.WebHost -reinstall
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.
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.
Try that. Good luck, it worked for me.
in my asp.net webform based on 4.5, adding System.Web.Routing dll and below import in global.aspx resolved the problem
<%# Import Namespace="System.Web.Routing" %>
<%# Import Namespace="System.Web.Http" %>
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.
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.
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.
In my case I was getting a similar issue in Glabal.asax:
The name 'AreaRegistration' does not exist in the current context.
I already had System.Web.Mvc namespace in Global.asax.cs but the compiler was somewhat not aware of it, that is, the AreaRegistration class was not being found.
What fixed the issue was executing the following command in Package Manager Console inside Visual Studio:
PM> Update-Package -reinstall
After NuGet reinstalled all the project's packages, the missing class names appeared and the code built successfully.
Something is broken in Visual Studio\NuGet restore pipeline...
Related
I'm trying to use Simple Membership in an MVC 5 project. I followed a video tutorial on a test project on my machine and was able to get it to work. Now I'm ready to implement it on a new website but when I get to the step of making a reference for Webmatrix.Data and Webmatrix.WebData I don't have the option in the References/Assemblies/Extensions section. I went to NuGet and tried to add WebMatrix and saw where is said it was legacy and to use AspNet.WebPages.WebData which I then installed through NuGet. Still no reference available for Webmatrix.
So, I looked at the references of the project that was working and found that there was a Packages/Microsoft.AspNet.WebPages.WebData.3.2.3 folder and copied that to my current projects folder. I then made a manual reference to the WebMatrix.dll in that folder and made sure that it was listed in my packages.config file like my working project. That seemed to have fixed my issue where it my pages were referencing the System.Web.Security.
However, in my Global.asax page I need to have the following
WebSecurity.InitializeDatabaseConnection("DbConnection", "Users", "UserId", "UserName", autoCreateTables: true);
I'm getting an error with that. It says 'object' does not contain a definition for 'InitializeDatabaseConnection and the using WebMatrix.WebData is grayed out at the top. When I type WebSecurity and look at the options through intellisense I have very few options and InitializeDatabaseConnection is definitely not there. I can see the reference to WebMatrix.Data and WebMatrix.WebData listed in my References section and there doesn't seem to be any errors associated with that.
I've tried to do some Google searches and can't find anything about the issue. I've been at this for several hours and I'm pulling my hair out trying to get this to work. Please, any suggests would be really helpful...I'm at a complete loss.
I had NuGet install Microsoft.AspNet.WebPages.WebData, Microsoft.AspNet.WebPages.Data and Microsoft.AspNet.WebPages (all v3.2.4).
Then I added a reference to WebMatrix.WebData by browsing to the package (.....\packages\Microsoft.AspNet.WebPages.WebData.3.2.4\lib\net45\WebMatrix.WebData.dll).
And everything worked fine, with
using System.Web.Security;
using WebMatrix.WebData;
My computer unexpectedly crashed while I was working in visual studio on a web application. As a result my csproj was completely corrupted, so I had to start a new web project and re-add all the files and assemblies from my old project.
But for some reason Route, which should be in System.Web.Http, and ResponseType, which should be in System.Web.Http.Description cannot be found, even though I still have the appropriate usings for them. Also, my project is still referencing System.Web and System.Web.Http, and all the other stuff in my application seems to not be generating any errors like this at the moment.
I've also tried writing out the full path to these classes (i.e. System.Web.Http.Description.ResponseType) but while intellisense is able to explore the namespace, the Route and ResponseType attributes are missing.
I'm not sure if this is related, but when I right-click on Controllers and select Add, I don't get an option to add a controller any more. Something got really messed up with this project...
You should re-install the necessary packages to enable Attribute Routing.
From the Tools menu in Visual Studio, select Library Package Manager, then select Package Manager Console. Enter the following command in the Package Manager Console window:
Install-Package Microsoft.AspNet.WebApi.WebHost
Then after manually adding relevant references (i.e System.Web.dll and System.Web.Http.dll if they aren't already referenced) and using proper namespaces, your attributes should work:
using System;
using System.Web.Http;
using System.Web.Http.Description;
As an alternative to the accepted answer, I found that it was possible to automatically have the necessary packages installed as soon as the first Web API 2 controller is added to the project through the solution explorer's context menu.
Resolve for me by doing the following!
Save the solution
update-package -reinstall
I am working on an Asp.Net MVC 4 Application in which I am using SignalR 2.0.1 and I Mapped it using Owin Startup class and it worked fine at first.
All of a sudden when I tried to rebuild my app it said that the type are namespace IAppbuilder could not be found.
Following is my start up class
using Microsoft.Owin;
using Owin;
using WhiteBoardApp;
namespace WhiteBoardApp
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
I have installed Owin package too, and for some reasons I could not find Owin Startup class so I just added a normal class and included all the references that are needed.
May I know where I am making a mistake
Try to use Package Manage Console and do
Update-Package Owin -Reinstall
I was having similar issue. But instead Owin, problem was causing Microsoft.Owin, obviously
Update-Package Owin -Reinstall
Didn't work, neither did Update-Package Owin
BUT
Install-Package Microsoft.Owin
did work fine for me, thanks.
The IAppBuilder interface is found under Owin package. Just add a reference in your class file:
using Owin;
And rebuild. Your project will pick this up.
I have no idea why VS didn't pick this up, but it didn't. Once I added this reference to my project, then everything fell into place.
I encountered the same problem while building my project. Here are the steps that helped fix my problem:
Go to Solution Explorer and look for your project
Under your project, expand the References; You should see warnings on the problematic reference
Right click References and open Manage NuGet Packages
Search the name of problematic reference i.e. Microsoft.Owin; After loading it shows that it is already installed (It is, but it installed incorrectly. Checking the properties > version at step 2 shows 0.0.0.0)
Check Force uninstall, even if there are dependencies on it
Uninstall
Install
Build and run the project
Problems
Cannot install Microsoft.Web.Infrastructure because it already exists in the packages folder. Rolling back...
Go to your project folder and look for packages
Find the problematic package i.e. Microsoft.Web.Infrastructure
Delete the folder
Resume from step 7
Alternatives
Here are the alternatives I've read about to fix this kind of problem.
Clean and Rebuild Project / Solution
Restart Visual Studio
Restart PC
Good luck.
My Visual Studio 2013 for some reason didn't realize that the references paths existed. The yellow exclamation mark in front of the references was shown for all the added packages. I checked ../packages/ but all files existed, i also opened the .csproj file which referenced the correct paths.
Closing and opening the solution returned quite a lot of errors, and could not load the projects included in the solution.
Restarting Visual Studio 2013 saved the day for some unexplained reason.
My following using's equivalent in F# present a problem of hiding the IAppBuilder. It turns out that the Owin stipulation was being interpreted as an incomplete System.Web.Http.Owin reference, even though the Owin.dll providing the Owin namespace was referenced.
open System.Net.Http
open System.Web.Http
open Microsoft.Owin
open Owin
The problem was resolved by rearranging the usings as follows:
open Microsoft.Owin
open Owin
open System.Net.Http
open System.Web.Http
...granted, this may be a bug peculiar to the F# compiler and name conflicts are handle better in C# and elsewhere.
In my case, I had moved around the project folders and the location of the vs solution file (.sln). Once I was done with re-adding the projects, there was a packages folder on the solution level and one was left in a project sub folder.
This way, in that project, the relative package folder links in the .csproj file got messed up.
The reinstallation or other tips regarding the nuget package manager in this thread were helpful. I noticed, that after I reinstalled a few packages, in my git source code diff, the path of the packages folder was changed within the csproj file.
Before
<HintPath>packages\Microsoft.Owin.4.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
After
<HintPath>..\packages\Microsoft.Owin.4.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
So, if you run in the same issue and you have a lot of nuget packages, it might be easier to close the whole solution, open the csproj file(s) in a text editor like vscode and fix the relative links with search and replace. Then just save, close, reopen solution in VS and restore nuget packages. That should do the trick.
(In any case, you should delete the local packages folder on the project level, so that the project really fails, if it does not get the right packages.)
It's an ordering issue.
using Microsoft.Owin;
using Owin;
Leads to Microsoft.Owin to be defined first, then Owin is found under already imported Microsoft namespace. If you mouse over Owin of using Owin you should see it was resolved to Microsoft.Owin again and furthermore IDE will gray out using Owin as redundant unused reference.
Do:
using global::Owin;
Which clarifies for the compiler not to look for Owin under already defined namespaces (e.g. Microsoft. namespace).
http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr
Check for the visual studio you are using
You can find the following comment
Note: If you are using Visual Studio 2012, the SignalR Hub Class (v2) template will not be available. You can add a plain Class called ChatHub instead.
Also
Note: If you are using Visual Studio 2012, the OWIN Startup Class template will not be available. You can add a plain Class called Startup instead.
I have downloaded, and successfully ran the ASP.NET Identity sample from here:
https://github.com/rustd/AspnetIdentitySample
I am now in the middle of implementing the ASP.NET Identity framework in my project and have ran into a problem, that has driven me mad all day...
GetOwinContext() does not exist as an extension method on my HttpContext
I am implementing the identity framework in class library. I have installed all the latest (pre-release version) of the Identity framework and everything - apart from this - is working fine.
I have tried implementing the same code as the same direct in my controller, and find the same problem.
I'm clearly missing a reference somewhere, though I have no idea what..!..
The code-block that is killing me is:
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
I have added references to the following - tried these both in my class library and also direct on the controller, none of them work for me...
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using Microsoft.Owin;
using System.Web;
... this is driving me up the wall....any idea?
UPDATE
I have checked the versions of Identity & OWIN in the sample, and I have made sure I have the same versions in my solution.
More so, if I search the object browser on the sample for GetOwinContext I can find the method, however when I search for it in my solution it is nowhere to be found... I must have some library out of date, but I can't find it!
ARGH!
I found it... I didn't have an extra package, called Microsoft.Owin.Host.SystemWeb
Once i searched and installed this, it worked.
Now - i am not sure if i just missed everything, though found NO reference to such a library or package when going through various tutorials. It also didn't get installed when i installed all this Identity framework... Not sure if it were just me..
EDIT
Although it's in the Microsoft.Owin.Host.SystemWeb assembly it is an extension method in the System.Web namespace, so you need to have the reference to the former, and be using the latter.
I believe you need to reference the current HttpContext if you are outside of the controller. The MVC controllers have a base reference to the current context. However, outside of that, you have to explicitly declare you want the current HttpContext
return HttpContext.Current.GetOwinContext().Authentication;
As for it not showing up, a new MVC 5 project template using the code you show above (the IAuthenticationManager) has the following using statements at the top of the account controller:
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using WebApplication2.Models;
Commenting out each one, it appears the GetOwinContext() is actually a part of the System.Web.Mvc assembly.
After trial and error comparing the using statements of my controller and the Asp.Net Template controller
using System.Web;
Solved the problem for me.
You are also going to need to add:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
To use GetUserManager method.
Microsoft couldn't find a way to resolve this automatically with right click and resolve like other missing using statements?
In my case adding Microsoft.AspNet.WebApi.Owin reference via nuget did the trick.
Make sure you installed the nuget package Microsoft.AspNet.Identity.Owin. Then add System.Net.Http namespace.
Just using
HttpContext.Current.GetOwinContext()
did the trick in my case.
For Devs getting this error in Web API Project -
The GetOwinContext extension method is defined in System.Web.Http.Owin dll and one more package will be needed i.e. Microsoft.Owin.Host.SystemWeb. This package needs to be installed in your project from nuget.
Link To Package: OWIN Package Install Command -
Install-Package Microsoft.AspNet.WebApi.Owin
Link To System.web Package : Package Install Command -
Install-Package Microsoft.Owin.Host.SystemWeb
In order to resolve this error you need to find why its occurring in your case. Please Cross check below points in your code -
You must have reference to Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity.Owin;
Define GetOwinContext() Under HttpContext.Current as below -
return _userManager1 ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
OR
return _signInManager ?? HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>();
Complete Code Where GetOwinContext() is used -
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
Namespace's I'm Using in Code File where GetOwinContext() Is used
using AngularJSAuthentication.API.Entities;
using AngularJSAuthentication.API.Models;
using HomeCinema.Common;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security.DataProtection;
I got this error while moving my code from my one project to another.
To get UserManager in API
return HttpContext.Current.GetOwinContext().GetUserManager<AppUserManager>();
where AppUserManager is the class that inherits from UserManager.
I had all the correct packages and usings, but had to built first before I could get GetOwinContext() to work.
Just install this package and your code will work:=>
Install-Package Microsoft.Owin.Host.SystemWeb -Version 2.1.0
I recently upgraded from Entities Framework 5 to Entities Framework 6 Alpha 2 and I am getting the following error:
Method not found: 'System.Data.Objects.ObjectContext
System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()'.
This is getting hit when I call
if (Membership.ValidateUser(model.UserName, model.Password)) {}
This used to work fine before not sure why it's springing this error. Any suggestions?
EF 6 does not have System.Data.Objects.ObjectContext. EF 6 has moved some types, including ObjectContext, from System.Data.Entity.dll into EntityFramework.dll, and changed their namespaces. The fact that you get this error suggests you haven't attempted to recompile your application, you've simply replaced EntityFramework.dll and hoped for the best. That won't work. You need to update your code to work with EF 6: you need to remove your references to System.Data.Entity.dll, and update your code to refer to the new types.
It just might be possible for the reference to the IObjectContextAdapter.ObjectContext property to be in some library you're using, but most likely it'll be in your own code. The error message (in the part you didn't include in your question) should tell you where it is coming from.
For me updating these below worked:
using System.Data.Objects; --> using System.Data.Entity.Core.Objects;
using System.Data.Objects.DataClasses; --> using System.Data.Entity.Core.Objects.DataClasses;
I'm also using EF 6.
I managed to solve the problem uninstalling the package Microsoft.AspNet.Providers.Core v. 1.2. I'm using version 1.1 instead. If you're like me and is using LocaDb, you'll have to uninstall the LocaDb package because it depends on that package. Of course you'll have to reinstall LocaDb again...
You can grab v. 1.1 using the NuGet Package Manager Console inside Visual Studio:
Install-Package Microsoft.AspNet.Providers.Core -Version 1.1
There's a Microsoft Connect bug filled regarding this issue:
Microsoft.AspNet.Providers.Core incompatible with EF6
The new 2.0 version of the providers (http://www.nuget.org/packages/Microsoft.AspNet.Providers.Core/) are EF6 compatible (they'll actually only work with EF6).
I managed to resolve this by removing the AspNet Providers I had installed through Nuget, which was marked as deprecated. Doing this also uninstalled Entity Framework.
I then installed the new AspNet Universal Providers, followed by Entity Framework 6, and all my problems were fixed.
Check This Link
http://visualstudiomagazine.com/articles/2014/03/01/whats-new-in-entity-framework-6.aspx
I Updated the EF 6.2 and get same error and find the solution as fallows
Change the namespace System.Data.Entity to System.Data.Entity.Core, including any references to System.Data.* namespaces (for example, System.Data.Objects becomes System.Data.Entity.Core.Objects).
That happens when entity framework is unable to find the method in the dotnet framework library installed in the machine. So install dotnet framework 4.5.2 or higher. It will fix the issue.
What worked for me was the following:
Install the dll 'Microsoft.AspNet.DataSource' with:
PM> Install-Package Microsoft.AspNet.EntityDataSource
Reference 'Microsoft.AspNet.DataSource.dll' in your project.
Added the following using declarations:
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using Microsoft.AspNet.EntityDataSource;
Removed the following using declarations:
using System.Data.Entity;
using System.Data.Metadata.Edm;
using System.Data.Objects;
using System.Web.UI.WebControls;
Voila, code is compiling and working.
It has an old version associated with edmx file for this:
Reinstall EF with Nuget
Delete the .edmx file and recreate it with tables
A quick and simple fix for me was to remove the offending assemblies (deprecated) and added a reference to the new library. The code was modified within the Context.tt and looks like this:
if (container.FunctionImports.Any())
{
#>
using System.Data.Entity.Core.Objects; // The assembly you need
using System.Linq;
<#
}
Before any modifications it had appeared as such:
if (container.FunctionImports.Any())
{
#>
using System.Data.Objects; // Error on compile
using System.Data.Objects.DataClasses; // Error on compile
using System.Linq;
<#