2013-04-10 43 views
6

我創建了一個新的asp.net mvc4應用程序。 我創建了一個控制器聊天和查看聊天。錯誤。在處理新的asp.net mvc4錯誤的請求時發生錯誤,找不到sql server?

public class ChatController : Controller 
    { 
     // 
     // GET: /Chat/ 

     public ActionResult Index() 
     { 
      return View(); 
     } 

    } 

我不會粘貼聊天視圖,因爲我不認爲它與錯誤無關。

在LoginPartial.cshtml我有這樣的:

@if (Request.IsAuthenticated) { 
    <text> 
     Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })! 
     @Html.ActionLink("Dashboard", "Index", "Dashboard", routeValues: null, htmlAttributes: new { id = "dashboard" }) 
     @Html.ActionLink("Chat", "Index", "Chat", routeValues: null, htmlAttributes: new { id = "chat" }) 

     @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) { 
      @Html.AntiForgeryToken() 
      <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a> 
     } 
    </text> 
} else { 
    <ul> 
     <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li> 
     <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li> 
    </ul> 
} 

瀏覽到應用程序和登錄電子後,我點擊聊天,我也得到一個: 錯誤。處理您的請求時發生錯誤。

通過使用NLOG我得到這個,我真的不知道是什麼代碼試圖連接到SQL,因爲聊天控制器沒有任何服務器端代碼做任何查詢

04/10/2013 20:17:44: System.Web.HttpException (0x80004005): Unable to connect to SQL Server database. ---> System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) 
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 
    at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) 
    at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) 
    at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) 
    at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) 
    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) 
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) 
    at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) 
    at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) 
    at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) 
    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) 
    at System.Data.SqlClient.SqlConnection.Open() 
    at System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) 
ClientConnectionId:00000000-0000-0000-0000-000000000000 
    at System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) 
    at System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) 
    at System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) 
    at System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) 
    at System.Web.DataAccess.SqlConnectionHelper.EnsureDBFile(String connectionString) 
    at System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation) 
    at System.Web.Security.SqlRoleProvider.GetRolesForUser(String username) 
    at WebMatrix.WebData.SimpleRoleProvider.GetRolesForUser(String username) 
    at System.Web.Security.RolePrincipal.IsInRole(String role) 
    at System.Web.Configuration.AuthorizationRule.IsTheUserInAnyRole(StringCollection roles, IPrincipal principal) 
    at System.Web.Configuration.AuthorizationRule.IsUserAllowed(IPrincipal user, String verb) 
    at System.Web.Configuration.AuthorizationRuleCollection.IsUserAllowed(IPrincipal user, String verb) 
    at System.Web.Security.UrlAuthorizationModule.OnEnter(Object source, EventArgs eventArgs) 
    at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

在web.config有這個,我檢查了App_data文件夾和MDF文件在那裏。

<connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-FullStackSampleApplication-20130404120217;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-FullStackSampleApplication-20130404120217.mdf" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

UPDATE: 通過檢查我的web.config,我看到elmah.axd取決於用戶和角色是受保護的,但沒有角色提供的任何地方,

<location path="elmah.axd" inheritInChildApplications="false"> 
    <system.web> 
     <httpHandlers> 
     <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> 
     </httpHandlers> 
     <authorization> 
     <allow roles="admin" /> 
     <allow users="levalencia" /> 
     <deny users="*" /> 
     </authorization> 
    </system.web> 
    <system.webServer> 
     <handlers> 
     <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" /> 
     </handlers> 
    </system.webServer> 
    </location> 
+0

是Web配置設置爲使用窗體身份驗證? – asawyer 2013-04-10 18:24:48

+0

是的。 2013-04-10 18:28:04

+0

確保您的項目中有「aspnet-FullStackSampleApplication-20130404120217.mdf」無障礙。 錯誤說明您的應用程序無法連接到SQL Server實例。 – HaBo 2013-04-10 18:29:19

回答

8

這是我如何設法解決問題:

  1. 我把customErrors關閉。

    <customErrors mode="Off" defaultRedirect="~/Error/General"> 
        <error statusCode="404" redirect="~/Error/Http404"/> 
        <error statusCode="403" redirect="~/Error/Http403"/>  
    </customErrors> 
    
  2. 然後我發現它試圖找到一個查看調用索引。

所以我改變了loginpartial.cshtml到

@Html.ActionLink("Chat", "Chat", "Chat", routeValues: null, htmlAttributes: new { id = "chat" }) 

我的錯誤被命名控制器和視圖。關於sql server的問題,我想應該是在應用程序中的其他東西在日誌上的某個地方。

+0

它可能正在嘗試寫一個日誌來告訴你這個錯誤。你真的沒有解決我害怕的問題。 – asawyer 2013-04-10 19:24:03

+0

+1將customErrors設爲Off – 2014-10-01 09:09:10

1

我解決了這個問題發表意見如下認證標籤:

<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login" timeout="2880" /> 

相關問題