简体   繁体   English

将MVC项目部署到IIS后出现500(内部服务器错误)

[英]500 (Internal Server Error) after deploy MVC project to IIS

My MVC project works well under VS2015 (LocalDB+IISexpress) also I use EntityFramework in this project. 我的MVC项目在VS2015(LocalDB + IISexpress)下运行良好,并且我在该项目中使用EntityFramework。

Goal: Now I want to try to publish it to local IIS.(ver.10) Then I attached the database in Microsoft SQL Management Studio 2016. I added the project in IIS. 目标:现在,我想尝试将其发布到本地IIS。(版本10)然后将数据库附加到Microsoft SQL Management Studio 2016中。我在IIS中添加了该项目。

Problem: When I call API controller it returns 500 error. 问题:当我调用API控制器时,它返回500错误。

    {"Message":"An error has occurred.","ExceptionMessage":"The underlying provider failed on ConnectionString.","ExceptionType":"System.Data.Entity.Core.EntityException","StackTrace":"   at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
   at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)
   at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)
   at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
   at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
   at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at WebApplication1.Controllers.BaseAPIController.ToJSON(Object obj)
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"The given path's format is not supported.","ExceptionType":"System.NotSupportedException","StackTrace":"   at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
   at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.Path.GetFullPath(String path)
   at System.Data.Common.ADP.GetFullPath(String filename)
   at System.Data.Common.DbConnectionOptions.ExpandDataDirectory(String keyword, String value, String& datadir)
   at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
   at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
   at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 interceptionContext)
   at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)"}}

In web.config my connection string is: 在web.config中,我的连接字符串是:

<connectionStrings>
<add name="UserDBEntities" connectionString="metadata=res://*/UserDbEntities.csdl|res://*/UserDbEntities.ssdl|res://*/UserDbEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\UserDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

In IIS in the "Connection Strings" settings the connection string after deploy looks like this: 在IIS的“连接字符串”设置中,部署后的连接字符串如下所示:

metadata=res://*/UserDbEntities.csdl|res://*/UserDbEntities.ssdl|res://*/UserDbEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\UserDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"

I change only source from source=(LocalDB)\\MSSQLLocalDB to source=DesktopName\\MSSQLSERVER2016 我仅将源从source =(LocalDB)\\ MSSQLLocalDB更改source = DesktopName \\ MSSQLSERVER2016

If you can open the database in Visual Studio, via the SQL Server Object Explorer pane, you can right-click on the database and choose "Properties". 如果可以通过“ SQL Server对象资源管理器”窗格在Visual Studio中打开数据库,则可以在数据库上单击鼠标右键,然后选择“属性”。 In the Properties pane, there will be a line with the connection string. 在“属性”窗格中,将在一行中包含连接字符串。

Otherwise, you'll need to manually construct the connection string, which for SQL Server, will likely look something like: 否则,您将需要手动构造连接字符串,对于SQL Server,该连接字符串可能类似于:

Data Source=[host];Initial Catalog=[DB];Integrated Security=False;User Id=[user];Password=[password];MultipleActiveResultSets=True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM