c# - 将 ASP MVC5 站点部署到 Azure,收到 500 内部服务器错误

标签 c# asp.net sql-server azure web-hosting

我使用本地数据库在本地构建了一个 ASP.NET MVC5 网站。我想将其引入 Azure Web 服务,因此我启用了 Azure 网站并设置从 Visual Studio Online Git 存储库进行部署。部署已完成,但访问该页面仅生成“处理您的请求时发生错误”。错误。

如果我查看日志,则会列出以下错误:

  1. MODULE_SET_RESPONSE_ERROR_STATUS

Warning ModuleName="ManagedPipelineHandler", Notification="EXECUTE_REQUEST_HANDLER", HttpStatus="500", HttpReason="Internal Server Error", HttpSubStatus="0", ErrorCode="The operation completed successfully. (0x0)", ConfigExceptionInfo=""

数据库的连接字符串已替换为 AzureSqlDatabase 连接字符串,因此我尝试检查那里。该字符串“看起来”很好,但我注意到,当我在 Debug模式下运行应用程序时,它以某种方式使用原始的本地 Sql 数据库,尽管该连接字符串不再存在于代码中!

我已经尝试了 SO 中列出的一些建议,包括启用详细日志记录和重新推送 web.config 文件,但似乎没有任何效果。关于如何追踪和解决此问题有什么想法吗?

编辑: 在 MFanto 的帮助下,我发现该应用程序仍然以某种方式使用旧的本地 SQL 数据库的连接字符串。 (数据源=.\SQLEXPRESS;初始目录=HouseOfBurt.Models.DataContext;集成安全性=True;MultipleActiveResultSets=True)

Web.config如下:

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="Application" />
        <add namespace="Application.Models" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*.cshtml" verb="*"
           preCondition="integratedMode"
           type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

Web.Debug.Config(当然要修改密码)

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
    finds an attribute "name" that has a value of "MyDB".

    <connectionStrings>
      <add name="MyDB"
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
  <connectionStrings>
    <add name ="DataContextContainer" connectionString="Server=tcp:r0c0umg8th.database.windows.net,1433;Database=HouseOfBurt;User ID=dba@r0c0cmg8th;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.EntityClient"/>
  </connectionStrings>

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire
      <customErrors> section of your Web.config file.
      Note that because there is only one customErrors section under the
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>

EDIT2:添加 DbContext 类:

public class DataContext : DbContext
    {
        #region Tables

        public DbSet<Article> Articles { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Link> Links { get; set; }

        #endregion Tables

        #region Public Methods

        #endregion Public Methods

    }

最佳答案

您发布的DbContext类名为DataContext,但示例中的连接字符串是DataContextContainer

如果未显式提供连接字符串, Entity Framework 将查找名称与 DBContext 名称完全匹配的连接字符串; else 约定将尝试使用基于代码的连接字符串,针对 SQLExpress。

更改连接字符串名称似乎已经解决了问题。

有关 Entity Framework 连接字符串相关约定的更多信息,请访问 http://msdn.microsoft.com/en-us/data/jj592674 .

关于c# - 将 ASP MVC5 站点部署到 Azure,收到 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25593861/

相关文章:

c# - 为什么函数指针不被认为是面向对象的?

java - 为什么 DES 加密和 DESede 加密结果相同?

c# - 更改特定列的方向 - 不是整行或表格

javascript - TypeError : sqlDb. 连接不是使用 Node.js 的 Rest Service 中的构造函数

c# - 如何将 Expression<Func<T, TProperty>> 转换为 Expression<Func<T, TNewProperty>>

html - ASP.Net 表格宽度未正确显示

c# - 如何检查 QueryString 是否没有参数

C#/SQL 语法错误

sql - 如何选择不匹配的行

c# - C# UserControl 上的 Jquery .ajax 异步回发