asp.net-mvc 调试 ASP.NET MVC 绑定的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4651085/
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
Best practices for debugging ASP.NET MVC Binding
提问by mklein
Can you give me any general advice on how to debug ASP.NET MVC Binding?
你能给我一些关于如何调试 ASP.NET MVC 绑定的一般建议吗?
When everything works as expected, ASP.NET MVC is great. But if something does not, like something doesn't bind for some unknown reason, I find it hard to trace down the problem and find myself spending hours tracking down a seemingly simple problem.
当一切都按预期工作时,ASP.NET MVC 很棒。但是,如果某些东西不具有约束力,例如某些东西由于某种未知原因而无法绑定,我会发现很难找到问题所在,并且发现自己要花费数小时来查找看似简单的问题。
Let's imagine you land in a controller method like this:
让我们想象一下,您进入了这样的控制器方法:
[HttpPost]
public ActionResult ShipmentDetails(Order order)
{
//do stuff
}
Let's further imagine that the Order class looks like this:
让我们进一步想象 Order 类如下所示:
public class Order
{
public decimal Total {get; set;}
public Customer Customer {get; set;}
}
public class Customer
{
public string Name {get; set;}
public string Phone {get; set;}
}
What are good places to start when Order
in the controller method is not bound correctly? What are good places to start when only parts of the Order
are bound correctly?
当Order
控制器方法没有正确绑定时,有什么好的开始?当只有部分Order
正确绑定时,有什么好的起点?
采纳答案by Russ Cam
As Darin has suggested, start with inspecting what is being sent from the client to the server using something like Firebug, Fiddler, or other web debugging proxy tool.
正如 Darin 所建议的那样,首先使用 Firebug、Fiddler 或其他 Web 调试代理工具检查从客户端发送到服务器的内容。
Failing that, you might want to step through the source code to see what's happening during binding.
如果做不到这一点,您可能希望单步执行源代码以查看绑定期间发生的情况。
Two ways that I can recommend doing this are
我可以推荐这样做的两种方法是
Include the System.Web.Mvc source code project in your application and reference this. This is good for learning but probably not recommended for a commerical application.
Download the symbols for System.Web.Mvc from the Microsoft Symbol servers, change your settings to be able to debug framework source code and set a break point appropriately to step through.
在您的应用程序中包含 System.Web.Mvc 源代码项目并引用它。这有利于学习,但可能不推荐用于商业应用。
从 Microsoft 符号服务器下载 System.Web.Mvc 的符号,更改设置以能够调试框架源代码并适当设置断点以单步执行.
回答by Kaleb Pederson
Although @russ's answeris useful and will sometimes be necessary, both options seem a little low level when the main question is more about the big picture. Thus, I'd recommend Glimpse.
尽管@russ 的回答很有用并且有时是必要的,但当主要问题更多地是关于大局时,这两个选项似乎都有些低级。因此,我推荐Glimpse。
From its about page:
从它的关于页面:
… Glimpse allows you to debug your web site or web service right in the browser. Glimpse allows you to "Glimpse" into what's going on in your web server. In other words what Firebug is to debugging your client side code, Glimpse is to debugging your server within the client.
... Glimpse 允许您直接在浏览器中调试您的网站或网络服务。Glimpse 允许您“瞥见”Web 服务器中发生的事情。换句话说,Firebug 是调试客户端代码,Glimpse 是调试客户端内的服务器。
And since you've specifically asked about data binding, you'll want to look at the binding tab documentation. You'll be able to see, again from the docs:
并且由于您专门询问了数据绑定,因此您需要查看绑定选项卡文档。您将能够再次从文档中看到:
- Ordinal: Order in which the MVC Model Binding infrastructure attempted to bind the available data
- Model Binder: Model Binder that was used in a given scenario
- Property/Parameter: Name of the thing that the Binder was trying to bind
- Type: Type of the thing that the Binder was trying to bind
- Attempted Value Providers: Providers that the Binder attempted to use to get a given value (and whether it was successful)
- Attempted Value: The actual value that the provider has to work with (post type conversation, etc.)
- Culture: The culture that was used to parse the raw value Raw Value: The raw value that the provider has to work with (pre type conversation, etc.)
- 序:顺序的MVC模型绑定基础设施试图绑定可用的数据
- 模型绑定器:在给定场景中使用的模型绑定器
- 属性/参数:Binder 试图绑定的事物的名称
- Type: Binder 试图绑定的东西的类型
- Attempted Value Providers: Binder 试图用来获取给定值的提供者(以及它是否成功)
- Attempted Value:提供者必须使用的实际价值(帖子类型对话等)
- 文化:用于解析原始值的文化原始值:提供者必须使用的原始值(预类型对话等)
See the quick start. Briefly:
请参阅快速入门。简要地:
- Install the glimpse.mvc3package
- Go to
http://yourhost/yourapp/Glimpse.axd
and "turn it on." - Click on the glimpse icon on the bottom right of any view in your app for details.
- 安装瞥见.mvc3包
- 转到
http://yourhost/yourapp/Glimpse.axd
并“打开它”。 - 单击应用程序中任何视图右下角的一瞥图标了解详细信息。
回答by Flores
回答by Darin Dimitrov
A good place to start is download and install FireBugand see what gets posted from the client to the server. Then you will see what's missing, incorrect, ... Blog posts such as Model Binding to a Listare good reads as well to get acquainted with the proper syntax that the default model binder uses.
一个好的起点是下载并安装FireBug,然后查看从客户端发送到服务器的内容。然后你会看到什么是缺失的、不正确的、...... 像模型绑定到列表这样的博客文章也是很好的阅读材料,可以熟悉默认模型绑定器使用的正确语法。
回答by John Peters
From Visual Studio side:
从 Visual Studio 方面:
- Set a breakpoint on entry to your endpoint.
- Open the Immediate via the Debug menu option.
- 在进入端点时设置断点。
- 通过调试菜单选项打开立即。
Type in ModelState.Keys.ToList()
输入 ModelState.Keys.ToList()
- This will show the binding errors by name/key.
- 这将按名称/键显示绑定错误。
Better yet type in ModelState.Values.ToList()...
最好输入 ModelState.Values.ToList()...