Fac*_*tic 3 c# asp.net asp.net-mvc iis-7 onactionexecuting
我想设置一个ASP.NET MVC路由,如下所示:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{idl}", // URL with parameters
new { controller = "Home", action = "Index", idl = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
路由请求看起来像这样......
Example/GetItems/1,2,3
Run Code Online (Sandbox Code Playgroud)
...到我的控制器动作:
public class ExampleController : Controller
{
public ActionResult GetItems(List<int> id_list)
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,我如何设置将idlurl参数从stringinto转换为List<int>并调用适当的控制器操作?
我在这里看到了一个相关的问题,用于OnActionExecuting预处理字符串,但没有改变类型.我不认为这对我有用,因为当我OnActionExecuting在我的控制器中覆盖并检查ActionExecutingContext参数时,我看到ActionParameters字典已经有idl一个空值的键 - 大概是从字符串尝试转换为List<int>...这个是我想要控制的路由的一部分.
这可能吗?
一个不错的版本是实现自己的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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3547 次 |
| 最近记录: |