How can I debug mvc4 razor views?

2019-06-20 04:55发布

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.

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#.

3条回答
Root(大扎)
2楼-- · 2019-06-20 05:21

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).

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.

  • Request is routed to your Controller
  • The Controller returns a ActionResult: return View(cnty);
  • MVC passes the ActionResult to your View
  • The exception occurs in your Index.cshtml when attempting to use the 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

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:

return View(cnty.ToList());

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

@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

<system.web>
    <customErrors mode ="Off" />
</system.web>
查看更多
爷、活的狠高调
3楼-- · 2019-06-20 05:32

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

@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.

查看更多
对你真心纯属浪费
4楼-- · 2019-06-20 05:36

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

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
            }
        }
查看更多
登录 后发表回答