ASP.NET核心 - 应用程序在发布后未连接到数据库

问题描述:

我使用用户身份验证创建了一个简单的ASP.Net Core应用程序(因此所有Entity Framework都已预先加载到Web应用程序模板中)。它使用 connectionString 连接到我的数据库,该文件位于我的 appsettings.json 文件中,data source = {computerName} \\ {serverName} setup。

I created a simple ASP.Net Core application with user authentication (so all the Entity Framework has been preloaded into the web app template). It is connecting to my database with the connectionString that is located in my appsettings.json file, with "data source = {computerName}\\{serverName}" setup.

数据库实例和Visual Studio位于同一台机器。在开发和调试模式下,这可以很好地将数据返回到web api。当我发布它并尝试从域名转到该站点时,它允许我查看不需要数据库连接的网页,但其余需要数据库的数据返回如下页面:

The database instance and Visual Studio are located on the same machine. This works fine in returning data to the web api while within development and debugging mode. When I publish it and try to go to the site from a domain name it does allow me to view the webpages that are not needing database connection but the rest that need data from the database returns pages like this:


错误。

处理您的请求时出错。
开发模式
交换到开发环境将显示有关发生的错误的更多详细信息。

Error.
An error occurred while processing your request. Development Mode Swapping to Development environment will display more detailed information about the error that occurred.

不应在已部署的
应用程序中启用开发环境,因为它可能会导致向最终用户显示
异常的敏感信息。对于本地调试,可以通过将
ASPNETCORE_ENVIRONMENT环境变量设置为Development来启用
开发环境,并将
重新启动应用程序。

Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application.

我试图将 ASPNETCORE_ENVIRONMENT 的环境变量从开发更改为生产,但没有成功。

I have tried to change the Environment Variables for ASPNETCORE_ENVIRONMENT from Development to Production to no success.

我尝试在我的项目中将 appsettings.Production.json 添加到 publishOptions .json 文件,即使没有文件 appsettings.Production.json ,这也无济于事。 dotnet publish

I have tried adding appsettings.Production.json to publishOptions in my project.json file, even though there is no file appsettings.Production.json, and this did not help. dotnet publish

web.config 文件中添加环境变量无效部署

Adding the evironment variable in the web.config file did not work Deployment

我需要帮助将已发布的web api从开发Visual Studio安装程序外部连接到我的SQL Server数据库。

I need help getting the published web api to connect to my SQL Server database from outside the development Visual Studio setup.

我能想到的最后一件事是,也许我我对连接字符串的理解不正确。如果web api使用连接字符串从服务器端连接到数据库,那么它应该像在开发中调用 http:// localhost:port#$ c一样正常工作$ c>因为它们都在同一台机器上。但是,如果数据库字符串需要基于客户端调用,则必须使用域名和IP地址。
有人可以告诉我它是哪一个吗?

The last thing I can think of is that maybe I am incorrect in how I understand the connection string. If the web api uses the connection string to connect to the database from the server-side then it should work just fine like it does when in development calling to http://localhost:port# since it's all on the same machine. But, if the database string needs to be based on a client-side call then it would have to be with domain names and IP addresses. Can someone tell me which one it is?

我想到的另一件事就是有些东西我没有做,需要做,在IIS管理器内部。我也看到了连接字符串,但由于连接字符串在应用程序内,因此不确定是什么。也许我想给应用程序一些授权与数据库服务器通信,即使它们在同一台机器上???

The only thing else that comes to mind is that there is something I am not doing, and need to do, inside of IIS Manager. I see connect string there as well but unsure what that is for our does since the connection string is inside the application. Also maybe I am suppose to give the app some kind of authorization to communicate with the database server even though they are on the same machine???

经过大量研究,最后用谷歌搜索如何在iis中部署web api我能够从使用Visual Studio Publish在IIS中托管ASP.NET Web API,我需要添加安全实体BUILTIN \\ \\IIS_IUSRS。然后将映射放到我的数据库表中,并将数据库的db_datareader(可能还有db_datawriter)提供给IUSER,以允许从我自己托管的IIS web api进行访问。这来自上面提到的链接部分陈述

After much research, finally googling "how to deploy a web api in iis" I was able to learn from Host ASP.NET Web API in IIS using Visual Studio Publish that I needed to add the security entity BUILTIN\IIS_IUSRS. Then placing a mapping to my database tables and giving db_datareader (and possibly db_datawriter) for the database to the IUSER to allow access from my self hosted IIS web api. This from the above mentioned link with the part stating


访问IIS下的数据库APPPOOL\ASP.NET v4.0
As我们使用的是ASP.NET v4.0应用程序池,确保将IIS APPPOOL\ASP.NET v4.0添加到数据库服务器 - >安全性 - >登录。

Accessing Database under IIS APPPOOL\ASP.NET v4.0 As we are using ASP.NET v4.0 App Pool make sure IIS APPPOOL\ASP.NET v4.0 is added to your Database Server -> Security -> Logins.