ASP.NET MVC 中 Controller.ReadFromRequest 的替代品是啥?

Posted

技术标签:

【中文标题】ASP.NET MVC 中 Controller.ReadFromRequest 的替代品是啥?【英文标题】:What is the replacement of Controller.ReadFromRequest in ASP.NET MVC?ASP.NET MVC 中 Controller.ReadFromRequest 的替代品是什么? 【发布时间】:2010-09-07 08:32:14 【问题描述】:

我正在尝试将项目从 ASP.NET MVC Preview 3 更新到 Preview 5,并且似乎 Controller.ReadFromRequest(string key) 已从 Controller 类中删除。有谁知道根据表单中的标识符检索信息的任何替代方法?

【问题讨论】:

【参考方案1】:

看起来他们已经添加了 controller.UpdateModel 来解决这个问题,签名是:

UpdateModel(object model, string[] keys)

我没有亲自升级我的应用程序,所以我不确定实际使用情况。我有兴趣自己了解一下,因为我也在使用controller.ReadFromRequest

【讨论】:

【参考方案2】:

不确定它去了哪里。不过,您可以推出自己的扩展程序:

公共静态类 MyBindingExtensions

public static T ReadFromRequest < T > (this Controller controller, string key) 

    // Setup
    HttpContextBase context = controller.ControllerContext.HttpContext;
    object val = null;
    T result = default(T);

    // Gaurd
    if (context == null)
        return result; // no point checking request

    // Bind value (check form then query string)
    if (context.Request.Form[key] != null)
        val = context.Request.Form[key];
    if (val == null) 
    
        if (context.Request.QueryString[key] != null)
            val = context.Request.QueryString[key];
    

    // Cast value
    if (val != null)
        result = (t)val;

    return result;



【讨论】:

【参考方案3】:

你能在 tinyurl.com 之类的地方重做那个链接吗?

我也需要这些信息,但可以让那个超级链接正常工作。

【讨论】:

以上是关于ASP.NET MVC 中 Controller.ReadFromRequest 的替代品是啥?的主要内容,如果未能解决你的问题,请参考以下文章

Asp.net MVC 中Controller返回值类型ActionResult

ASP.NET MVC 中 Controller.ReadFromRequest 的替代品是啥?

Asp.net MVC中 Controller 与 View之间的数据传递

ASP.NET MVC 4 中 Controller 与 ApiController 做读取新增更新删除 ( CRUD )

ASP.NET MVC中Controller与View之间的数据传递

asp.net mvc中Controller如何返回DataTable给页面