【问题标题】: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 类中删除。有谁知道根据表单中的标识符检索信息的任何替代方法?

【问题讨论】:

    标签: c# asp.net-mvc .net-3.5 entity-framework-ctp5


    【解决方案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 之类的地方重做那个链接吗?

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-20
          • 1970-01-01
          • 1970-01-01
          • 2011-02-03
          • 2010-09-22
          • 2018-07-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多