【问题标题】:ASP.NET MVC controller actions with custom parameter conversion?具有自定义参数转换的 ASP.NET MVC 控制器操作?
【发布时间】:2011-12-06 21:00:33
【问题描述】:

我想设置一个 ASP.NET MVC 路由,如下所示:

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{idl}", // URL with parameters
  new { controller = "Home", action = "Index", idl = UrlParameter.Optional } // Parameter defaults
);

路由看起来像这样的请求...

Example/GetItems/1,2,3

...到我的控制器操作:

public class ExampleController : Controller
{
    public ActionResult GetItems(List<int> id_list)
    {
        return View();
    }
}

问题是,我该如何设置才能将idl url 参数从string 转换为List&lt;int&gt; 并调用适当的控制器操作?

我见过related question here 使用OnActionExecuting 预处理字符串,但没有更改类型。我不认为这对我有用,因为当我在控制器中覆盖 OnActionExecuting 并检查 ActionExecutingContext 参数时,我看到 ActionParameters 字典已经有一个带有空值的 idl 键 -据推测,尝试从字符串转换为 List&lt;int&gt;... 这是我想要控制的路由部分。

这可能吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc iis-7 onactionexecuting


    【解决方案1】:

    一个不错的版本是实现您自己的模型绑定器。您可以找到样本here

    我试着给你一个想法:

    public class MyListBinder : IModelBinder
    {   
         public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
         {   
            string integers = controllerContext.RouteData.Values["idl"] as string;
            string [] stringArray = integers.Split(',');
            var list = new List<int>();
            foreach (string s in stringArray)
            {
               list.Add(int.Parse(s));
            }
            return list;  
         }  
    }
    
    
    public ActionResult GetItems([ModelBinder(typeof(MyListBinder))]List<int> id_list) 
    { 
        return View(); 
    } 
    

    【讨论】:

      【解决方案2】:

      就像 slfan 所说,自定义模型绑定器是可行的方法。这是另一种方法from my blog,它是通用的并且支持多种数据类型。它还优雅地回退到默认模型绑定实现:

      public class CommaSeparatedValuesModelBinder : DefaultModelBinder
      {
          private static readonly MethodInfo ToArrayMethod = typeof(Enumerable).GetMethod("ToArray");
      
          protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
          {
              if (propertyDescriptor.PropertyType.GetInterface(typeof(IEnumerable).Name) != null)
              {
                  var actualValue = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
      
                  if (actualValue != null && !String.IsNullOrWhiteSpace(actualValue.AttemptedValue) && actualValue.AttemptedValue.Contains(","))
                  {
                      var valueType = propertyDescriptor.PropertyType.GetElementType() ?? propertyDescriptor.PropertyType.GetGenericArguments().FirstOrDefault();
      
                      if (valueType != null && valueType.GetInterface(typeof(IConvertible).Name) != null)
                      {
                          var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(valueType));
      
                          foreach (var splitValue in actualValue.AttemptedValue.Split(new[] { ',' }))
                          {
                              list.Add(Convert.ChangeType(splitValue, valueType));
                          }
      
                          if (propertyDescriptor.PropertyType.IsArray)
                          {
                              return ToArrayMethod.MakeGenericMethod(valueType).Invoke(this, new[] { list });
                          }
                          else
                          {
                              return list;
                          }
                      }
                  }
              }
      
              return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多