sanvj sanvj - 1 year ago 317
ASP.NET (C#) Question

"WebSecurity" does not exist in the current context error in _AppStart.cshtml

I am getting the following compilation error:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'WebSecurity' does not exist in the current context

Source Error:

Line 1: @{
Line 2: if (!WebSecurity.Initialized)
Line 3: {
Line 4: WebSecurity.InitializeDatabaseConnection("AreaProject", "User", "UserId", "EmailAddress", autoCreateTables: true);

Source File: c:\Projects\area\trunk\dotNet\area.Web\area.Web\_AppStart.cshtml Line: 2
enter code here

My project is correctly referencing to "System.Web" and my other files that are using " System.Web.Security" are compiling fine.

The problem started when I added the references System.Web, System.Web.Pages and System.Web.Razor to my application. But I didn't make any change to WebMatrix.WebData or WebMatrix.Data.

Answer Source

The WebSecurity class that is being referenced here is apart of WebMatrix.WebData and not System.Web or System.Web.Security. You probably need to add a reference to WebMatrix.WebData.dll and things should at least get past the runtime compiler error.

You get the compiler error at runtime because views are normally compiled when the application starts up for the first time, instead of when the assemblies are complied.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download