การกระทำของคอนโทรลเลอร์ 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();
    }
}

คำถามคือ ฉันต้องตั้งค่าอะไรเพื่อแปลงพารามิเตอร์ idl url จาก string เป็น List<int> และเรียกใช้การทำงานของคอนโทรลเลอร์ที่เหมาะสม

ฉันได้เห็นคำถามที่เกี่ยวข้องที่นี่ ซึ่งใช้ OnActionExecuting เพื่อประมวลผลสตริงล่วงหน้า แต่ ไม่ได้เปลี่ยนประเภท ฉันไม่คิดว่ามันจะได้ผลสำหรับฉันที่นี่ เพราะเมื่อฉันแทนที่ OnActionExecuting ในคอนโทรลเลอร์ของฉันและตรวจสอบพารามิเตอร์ ActionExecutingContext ฉันเห็นว่าพจนานุกรม ActionParameters มีคีย์ idl ที่มีค่า Null อยู่แล้ว ซึ่งน่าจะเป็นการพยายามส่งจากสตริงไปยัง List<int>... นี่เป็นส่วนหนึ่งของเส้นทางที่ฉันต้องการควบคุม

เป็นไปได้ไหม?


person Factor Mystic    schedule 06.12.2011    source แหล่งที่มา


คำตอบ (2)


เวอร์ชันที่ดีคือการใช้ Model Binder ของคุณเอง คุณสามารถดูตัวอย่างได้ที่นี่

ฉันพยายามที่จะให้ความคิดแก่คุณ:

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