ASP.NET 移动应用因请求简单的 GET 操作而出现“500 内部服务器错误”

ASP.NET Mobile App getting "500 Internal Sever Error" from requesting a simple GET action

我在 Visual Studio 中创建了一个 ASP.NET Web 应用程序 - Azure 移动应用程序项目。我没有修改任何东西,所以它有默认的待办事项控制器。如果我 运行 在本地程序并使用 REST 客户端从

请求 GET 操作

localhost:port/tables/TodoItem

header ZUMO-API-VERSION 设置为 2.0.0 一切正常。我将应用程序发布到 Azure,当我尝试从

请求 GET 操作时

MYLINK.azurewebsites.net/tables/TodoItem

header ZUMO-API-VERSION 设置为 2.0.0 我得到(大约 1 分钟后)消息为 "An error has occurred." 的“500 内部服务器错误”如果我不添加 header,我几乎立即收到“400 错误请求”,说我必须指定 API 版本.难道我做错了什么?是什么导致了这个问题,我该如何解决?

发生错误是因为 Azure 为后端创建的默认项目旨在与 SQL 一起使用。您可以利用 KUDU or Azure App Service Editor 检查 web.config 文件下的 MS_TableConnectionString 连接字符串。连接字符串如下所示:

<add name="MS_TableConnectionString" connectionString="Server=tcp:{your_dbservername}.database.windows.net,1433;Initial Catalog={your_dbname};Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient"/>

当通过 Visual Studio 将您的移动应用程序部署到 Azure 时,您需要将连接字符串指向您的 Azure 数据库,如下所示:

有关创建移动应用并将其部署到 Azure 应用服务的更多详细信息,您可以参考此 document. Also, you could leverage "All Settings > Application settings" under your web app blade to configure your connectionstring, which could override your existing connectionstring in your web.config file at runtime. For more details, you could follow this official tutorial

配置好之后就可以正常工作了。 此外,您可以关注 Adrian Hall's book here 以更好地理解和快速开始使用 Azure 移动应用程序。