2013-07-11 30 views
1

我收到以下編譯錯誤:「WebSecurity」並不在目前的情況下錯誤存在於_AppStart.cshtml

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 

我的項目是正確引用到「的System.Web」我是其他文件使用「System.Web.Security」正在編譯好。

當我將System.Web,System.Web.Pages和System.Web.Razor引用到我的應用程序時,問題就開始了。但是我沒有對WebMatrix.WebData或WebMatrix.Data進行任何更改。

回答

5

此處引用的WebSecurity類是WebMatrix.WebData的一部分,而不是System.WebSystem.Web.Security。您可能需要添加對WebMatrix.WebData.dll的引用,並且事情至少應該通過運行時編譯器錯誤。

在運行時會出現編譯器錯誤,因爲視圖通常是在應用程序第一次啓動時編譯的,而不是編譯程序集時。

+0

我已經有了對WebMatrix.WebData.dll的引用,但我仍然收到相同的錯誤。 – sanvj

+0

您是否在'_AppStart.cshtml'的頂部添加了@using WebMatrix.WebData',或者將其作爲剃鬚刀名稱空間添加到web.config中? –

+1

我跟着你的步驟,但現在我得到這個錯誤「CS0117:'WebMatrix.WebData.WebSecurity'不包含'初始化'的定義」。我檢查了WebMatrix.WebData的版本和它的正確版本2. – sanvj

1

我假設你正嘗試第一次運行MVC4應用程序。

MVC4爲Internet Application模板添加了一系列裝配。如果你不使用它,它會變成PIA。

基本上,如果您不使用WebMatrix或OAuth進行身份驗證,您可以註釋掉SimpleMembershipInitializer類中的所有內容。

private class SimpleMembershipInitializer 
{ 
    // Comment out everything 
} 

只是爲了測試,你也可以註釋掉在AccountController裏面的每一行都會拋出異常。

+0

是的,這是我第一次使用MVC4。但我正在使用SimpleMembershipProvider爲我的應用程序提供身份驗證。 – sanvj

1

舊線程,但以防萬一它在路上幫助某人。我不確定它爲什麼這樣工作,但有時候安裝像WebMatrix.WebData,WebMatrix.Data這樣的軟件包還不夠。當我在安裝這些程序集後查看應用程序的Reference部分時,WebMatrix.WebData,WebMatrix.Data仍然沒有顯示,儘管它們位於應用程序的BIN文件夾中。我不得不從字面上使用添加引用來製作它們。只有這樣,他們纔出現,一切都開始工作。

相關問題