C# 如何调试 mvc4 razor 视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17281721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:01:26  来源:igfitidea点击:

How can I debug mvc4 razor views?

c#asp.net-mvcdebuggingrazor

提问by Pakk

I'm used to C# and vb.net winforms, and usually can find all the errors I need just by setting a breakpoint and stepping through my code.

我习惯了 C# 和 vb.net winforms,通常可以通过设置断点和单步执行我的代码来找到我需要的所有错误。

I would like to know what I'm doing wrong.

我想知道我做错了什么。

I'm placing a breakpoint here:

我在这里放置一个断点:

public ActionResult Index(int id)
{
    var cnty = from r in db.Clients
               where r.ClientID == id
               select r;

    if (cnty != null) // breakpoint here
    {
        return View(cnty); // F11 jumps over this section of code returning me to the error page below.
    }
    return HttpNotFound();
}

Yet again I have no clue where or why it errored out exactly. How can I find out why or better yet what error it is throwing?

再一次,我不知道它究竟在哪里或为什么出错。我怎样才能找出它抛出的原因或更好的错误?

I'm using VS2012 mvc4 c#.

我正在使用 VS2012 mvc4 c#。

采纳答案by David L

You need to put breakpoints in your view itself. You can place breakpoints on anything using razor syntax such as:

您需要在视图本身中放置断点。您可以使用 razor 语法在任何东西上放置断点,例如:

@Html.ActionLink
@{ var x = model.x; }

If you are getting a null reference exception, put breakpoints on places where you consume the model in your view.

如果您收到空引用异常,请在视图中使用模型的位置放置断点。

回答by Dave Swersky

First, use a tryblock. Your exception will be available in the catch block for inspection, reporting, etc.

首先,使用try块。您的异常将在 catch 块中可用以进行检查、报告等。

public ActionResult Index(int id)
        {
            try
            {
            var cnty = from r in db.Clients
                       where r.ClientID == id
                       select r;

            if (cnty != null) // breakpoint here
            {
                return View(cnty); // F11 jumps over this section of code returning me to the error page below.
            }
            return HttpNotFound();
            }
            catch (Exception ex)
            { 
                  //report error
            }
        }

回答by Ed Chapel

It would help to see the exception you are seeing. I'm guessing that you are seeing an exception when the page renders. As "David L" identified above, you want to set your breakpoint in the Razor view (Index.cshtml).

查看您所看到的异常会有所帮助。我猜您在页面呈现时会看到异常。正如上面提到的“David L”,您想在 Razor 视图 ( Index.cshtml) 中设置断点。

But why?

但为什么?

It helps to understand the lifecycle of a request/response in MVC. Here is the first example I found with a visual. There are sure to be others.

它有助于理解 MVC 中请求/响应的生命周期。这是我发现第一个带有视觉效果的示例。肯定还有其他人。

  • Request is routed to your Controller
  • The Controller returns a ActionResult: return View(cnty);
  • MVC passes the ActionResultto your View
  • The exception occurs in your Index.cshtmlwhen attempting to use the ActionResult.
  • 请求被路由到您的控制器
  • 控制器返回一个ActionResultreturn View(cnty);
  • MVC 将传递ActionResult给您的视图
  • 在发生异常Index.cshtml试图使用的时候ActionResult

I'm going to speculate that it has something to do with a disposed DB context object. Depending on the ORM you are using, the result of

我将推测它与已处理的 DB 上下文对象有关。根据您使用的 ORM,结果

from r in db.Clients
where r.ClientID == id
select r

is an IQueryable<Client>. You may be surprised to find out that your code has not yet contacted the database before return View(cnty);is executed. Try this instead:

是一个IQueryable<Client>。您可能会惊讶地发现您的代码在return View(cnty);执行之前还没有联系数据库。试试这个:

return View(cnty.ToList());

Again, the exact error you are seeing is important. My suggestion presumes Index.cshtmlbegins with:

同样,您看到的确切错误很重要。我的建议假设Index.cshtml开始于:

@model IEnumerable<Client>

Update:

更新:

Per OP's comment below, the stack trace is not available. There are many questions dedicated to seeing the stack trace in your browser during development. At least confirm that the following is set in your web.config

根据下面 OP 的评论,堆栈跟踪不可用。有很多问题专门用于在开发过程中查看浏览器中的堆栈跟踪。至少确认以下设置在您的web.config

<system.web>
    <customErrors mode ="Off" />
</system.web>