【问题标题】:Error. An error occurred while processing your request in new asp.net mvc4 error, sql server not found?错误。在新的 asp.net mvc4 错误中处理您的请求时出错,找不到 sql server?
【发布时间】:2016-05-14 08:44:38
【问题描述】:

我创建了一个新的 asp.net mvc4 应用程序。 然后我创建了一个控制器 Chat 和一个 View Chat。

 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>

更新: 通过检查我的 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>

【问题讨论】:

  • 网络配置是否设置为使用表单身份验证?
  • 是的。
  • 确保您的项目中有“aspnet-FullStackSampleApplication-20130404120217.mdf”并且可以访问。错误说明您的应用程序无法连接到 SQL Server 实例。
  • 是的,我知道,但该文件在 app_data 文件夹中,它是标准的 asp.net mvc 4 互联网模板。我没有做任何大的改变。
  • 您是否检查了 global.asax 中的 Application_Start() 并查看它是否正在尝试在那里初始化任何数据库连接?

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

这就是我设法解决问题的方法:

  1. 我将 customErrors 设置为 Off。

    <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 问题,我想它在应用程序中某处的日志中的其他内容。

【讨论】:

  • 它可能试图写一个日志来告诉你这个错误。恐怕你真的没有解决问题。
【解决方案2】:

我解决了这个问题以评论以下身份验证标签:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多