gpt4 book ai didi

asp.net-mvc - 调试 ASP.NET MVC 绑定(bind)的最佳实践

转载 作者:行者123 更新时间:2023-12-03 06:10:42 25 4
gpt4 key购买 nike

您能给我有关如何调试 ASP.NET MVC 绑定(bind)的一般建议吗?

当一切按预期工作时,ASP.NET MVC 就很棒。但是,如果某些东西没有绑定(bind),比如某些东西由于某种未知原因而没有绑定(bind),我会发现很难追踪问题,并发现自己花了几个小时来追踪一个看似简单的问题。

假设您进入了这样的 Controller 方法:

[HttpPost]
public ActionResult ShipmentDetails(Order order)
{
//do stuff
}

让我们进一步想象一下 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;}
}

当 Controller 方法中的 Order 未正确绑定(bind)时,有哪些好的起点?当仅正确绑定(bind)了部分 Order 时,从哪些地方开始比较好?

最佳答案

虽然@russ's answer很有用,有时是必要的,当主要问题更多的是关于大局时,这两个选项似乎都有点低级。因此,我推荐Glimpse .

从其“关于”页面:

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

由于您专门询问了数据绑定(bind),因此您需要查看 binding tab documentation 。您将能够再次从文档中看到:

  1. 序数:MVC 模型绑定(bind)基础结构尝试绑定(bind)可用数据的顺序
  2. 模型绑定(bind)器:在给定场景中使用的模型绑定(bind)器
  3. 属性/参数:Binder 尝试绑定(bind)的事物的名称
  4. 类型:Binder 尝试绑定(bind)的事物的类型
  5. 尝试的值提供者:Binder 尝试用来获取给定值的提供者(以及是否成功)
  6. 尝试值:提供者必须使用的实际值(帖子类型对话等)
  7. 文化:用于解析原始值的文化原始值:提供者必须使用的原始值(预类型对话等)

请参阅quick start 。简而言之:

  1. 安装glimpse.mvc3
  2. 转到 http://yourhost/yourapp/Glimpse.axd 并“将其打开”。
  3. 点击应用中任意 View 右下角的一瞥图标即可了解详细信息。

关于asp.net-mvc - 调试 ASP.NET MVC 绑定(bind)的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4651085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com