我希望有可能通过以下URL类型访问操作:
http://localhost/MyControllerName/MyActionName/Id1+Id2+Id3+Id4等
并按以下方式在代码中处理它:
public ActionResult MyActionName(string[] ids) { return View(ids); }
解决方法
是网址中的保留符号.这意味着白色空间.因此,为了实现您所寻找的目标,您可以编写自定义模型绑定器:
public class StringModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (value != null && !string.IsNullOrEmpty(value.AttemptedValue)) { return value.AttemptedValue.Split(' '); } return base.BindModel(controllerContext,bindingContext); } }
然后全局注册string []类型或使用ModelBinder
属性:
public ActionResult MyActionName( [ModelBinder(typeof(StringModelBinder))] string[] ids ) { return View(ids); }
显然,如果你想使用形式为/ MyControllerName / MyActionName / Id1 Id2 Id3 Id4的url将最后一部分绑定为一个名为ids的动作参数,你必须修改使用{id}的默认路由定义.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。