[英].NET MVC Routing - bind 2 routeMap parameters
我需要像下面这样解析一个 url
/controller/action/subaction/id
目前我正在使用子动作开关来查看实际需要做什么。 例如:
public ActionResult Members(string subaction, long id=0)
{
switch (subaction)
{
case "Details":
var member = _payment.GetMember(id);
return View("Members_details", member);
default:
var members = _payment.GetMembers().ToList();
return View("Members_list", members);
}
}
这可行,但我宁愿对每个事件都有单独的操作,直接从路由访问。 如果可能的话,我想结合路由图中的动作和子动作来访问正确的动作。
可以直接从路线图吗?
如果我是你,我会编写一个动作方法选择器并使用它来避免你的动作分支。 我写了一个分离带有可选参数的动作(从而避免动作中的分支代码 - 简化的单元测试)。 默认 Asp.net MVC 定义的路由有一个可选的id
参数
{controller}/{action}/{id}
id = UrlParameter.Optional
所以有这两种操作方法是有意义的:
public ActionResult Index() { ... }
public ActionResult Index(int id) { ... }
我通过编写自定义操作选择器过滤器来实现这一点。 这是一篇详细的博客文章,描述了整个事情并提供了一些您可以查看的代码。
在您的情况下,这意味着您必须编写一个名为SubactionAttribute
的自定义操作方法选择器 class ,然后简单地用它装饰您的操作:
[Subaction("Details")]
public ActionResult Members(long id)
{
var member = _payment.GetMember(id);
return View("Members.Details", member);
}
[Subaction] // no name would mean default subaction (when not provided)
public ActionResult Members()
{
var members = _payment.GetMembers().ToList();
return View("Members.List", members);
}
我不会为您编写整个 class,但我只会为您指明正确的方向,以便您可以按照相同的路径到达您的目标:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public sealed class SubactionAttribute : ActionMethodSelectorAttribute
{
#region Properties
/// <summary>
/// Gets subaction name.
/// </summary>
public string Name { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SubactionAttribute"/> class.
/// </summary>
public SubactionAttribute()
: this(null)
{
// does nothing
}
/// <summary>
/// Initializes a new instance of the <see cref="SubactionAttribute"/> class.
/// </summary>
/// <param name="subactionName">Sub-action name</param>
public SubactionAttribute(string subactionName)
{
this.Name = subactionName;
}
#endregion
#region ActionMethodSelectorAttribute implementation
/// <summary>
/// Determines whether the action method selection is valid for the specified controller context.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="methodInfo">Information about the action method.</param>
/// <returns>
/// true if the action method selection is valid for the specified controller context; otherwise, false.
/// </returns>
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
// get the value of subaction here
string subName = /* this part you'll have to write */
// determine whether subaction matches
return this.Name == subName;
}
#endregion
}
您是说您实际上想在同一个 controller 中使用多个“详细信息”操作吗? 如果是这种情况,那么我认为这是开始拆分控制器的一个很好的理由。
您可能应该从称为具有操作详细信息和列表(或索引)的成员的 Controller 开始
/Members/Details/ID
/Members/List/
您是否有充分的合乎逻辑的理由将它们全部放在同一个 controller 中?
或者,您可以称您为 Actions MemberDetails 和 MemberList 和 URL 之类的......
/Controller/MemberDetails/ID
/Controller/MemberList/
给它一个 go 代码:
[ActionName("Member/Details")]
public ActionResult Members_Details(int id){
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.