【问题标题】:Azure - Object reference not set to an instance of an objectAzure - 对象引用未设置为对象的实例
【发布时间】:2015-04-30 09:14:28
【问题描述】:

在发布到 Azure 后,我的 MVC 网站出现问题。

虽然在本地测试一切正常,但在我发布所有带有模型的页面后停止工作并在我使用模型的行上给出“NullReferenceException”,例如

@For Each item In Model

可能是我忘记在 Web.config 中添加一些内容了吗?

附:如果你能告诉我如何使用 breakpionts 调试 azure 网站,那将是完美的。

更新:

NewsContoller.vb

Imports System
Imports System.Linq
Imports System.Web.Mvc
Imports website.Models.Objects.Collections
Imports website.Models.Logic
Imports website.Models.Objects

Namespace Controllers
    Public Class NewsController
        Inherits Controller

        ' GET: News
        Function Index(Optional ByVal id As Integer = 0) As ActionResult
            Dim model As New NewsList
            model = NewsManager.GetList(id)
            Return View(model)
        End Function

        ' GET: News/Details/id
        Function Details(ByVal id As Integer) As ActionResult
            Dim model As New News
            model = NewsManager.GetItem(id)
            Return View(model)
        End Function
    End Class
End Namespace

新闻/Index.vbhtml

@ModelType List(Of website.Models.Objects.News)

<div style="width:100%; border-top:1px solid #e1e1e1;"></div>
<div class="content-wrapper">
    <h2 style="margin-top:0;">News</h2>
    <div style="position:relative;">
        <ul id="news-container" style="visibility:hidden;">
            @For Each item In Model
                @<li>
                     <a href="/News/Details/@item.NewsID">
                         <figure class="tint">
                             <img src='~/Uploads/News/@String.Concat(item.Pictures(0).PictureGUID,".jpg")' width="243" />
                         </figure>
                         <div class="date-container">@item.PostDate.ToString("%d")<br />@item.PostDate.ToString("MMM")</div>
                         <h3>@item.Title</h3>
                         <p style="width:233px; padding:0 5px; margin:5px 0;">@item.Description</p>
                     </a>
                </li>
            Next
        </ul>
    </div>

NewsManager.GetList() 从数据库中返回一个 List(of News)。

【问题讨论】:

  • 你的模型有什么方法实际上是从控制器返回为 null 的吗?你是如何发布你的网站的?您以前发布过它还是这是一个新的?您可以通过查看->服务器资源管理器->网站->选择一个网站->附加调试器进行调试。 azure.microsoft.com/en-us/documentation/articles/…
  • 没办法。这些页面在本地工作。我从 Visual Studio 发布它。我之前发布了它,但没有使用模型的页面。我试着做到了,我有窗口“检索远程调试器设置”然后浏览器窗口打开了,仅此而已。
  • 发布时请注意在设置中设置为调试器模式。还可能需要检查设置中的发布复选框时删除旧文件。
  • 仍然对我不起作用。使用我理解的一些测试页面,该控制器无法访问模型。肯定有些东西在模型内部不起作用。但我仍然无法使用断点进行调试。
  • 为了语义,控制器不能访问模型,控制器创建模型。视图是访问它的人(通过剃刀引擎)。发布更多代码,让我们看看它的去向。还尝试在 Model??new List() 中编写 @Foreach var item 以便它运行

标签: vb.net azure model-view-controller


【解决方案1】:

虽然没有看到NewsManager.GetList() 的代码就无法分辨,但NewsManager.GetList(id) 可能返回null 对象而不是空白列表。这可能是由于NewManager 处理不良数据库连接的方式,或者您的数据库表在 Azure 中创建后没有任何数据。

您可以通过执行以下操作来复制问题:

Function Index(Optional ByVal id As Integer = 0) As ActionResult
        Dim model As New NewsList
        model = Nothing 'simulate NewsManager.GetList(id) returning null 
        Return View(model)
End Function

正如@misha130 在他的评论中提到的,您将找到有关如何在 Azure here 中调试网站的详细说明。

基本步骤是:

  1. 打开网络项目
  2. 在有问题的行上设置断点。
  3. 右键单击项目并选择发布。
  4. 对于正确的配置文件,单击“设置”选项卡,将“配置”更改为“调试”,然后单击“发布”。
  5. 在服务器资源管理器中展开 Azure,展开 Web 应用程序,右键单击您的 Web 应用程序,然后单击附加调试器。 (可能会有一点延迟)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多