Действия контроллера ASP.NET MVC с преобразованием настраиваемых параметров?

Я хочу настроить маршрут 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();
    }
}

Вопрос в том, что мне настроить для преобразования параметра URL idl из string в List<int> и вызова соответствующего действия контроллера?

Я видел здесь связанный вопрос, который использовал OnActionExecuting для предварительной обработки строки, но не менял тип. Я не думаю, что это сработает для меня здесь, потому что, когда я переопределяю OnActionExecuting в моем контроллере и проверяю параметр ActionExecutingContext, я вижу, что в словаре ActionParameters уже есть idl ключ с нулевым значением - предположительно, попытка преобразования строки в _12 _... это часть маршрутизации, которую я хочу контролировать.

Это возможно?


person Factor Mystic    schedule 06.12.2011    source источник


Ответы (2)


Хорошая версия - реализовать собственный связыватель моделей. Вы можете найти образец здесь

Я пытаюсь дать вам представление:

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(); 
} 
person slfan    schedule 06.12.2011

Как говорит slfan, подшивка нестандартной модели - лучший вариант. Вот еще один подход, из моего блога который является универсальным и поддерживает несколько типов данных. Он также элегантно возвращается к реализации привязки модели по умолчанию:

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);
    }
}
person Nathan Taylor    schedule 07.12.2011