.NET MVC路由 - 绑定2个routeMap参数

时间:2011-07-13 14:40:43

标签: c# asp.net-mvc routes

我需要像以下

一样解析网址
/controller/action/subaction/id

目前我正在使用子操作开关来查看实际需要完成的操作。 EG:

    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);
        }
    }

这有效,但我宁愿为每个事件分别采取行动,直接从路线进入。如果可能的话,我想在路线图中组合动作和子动作来访问正确的动作。

  • / controller / action /将调用action()
  • / controller / action / subaction将调用action_subaction()
  • / controller / action / subaction / id将调用action_subaction(id)

这可以直接来自路线图吗?

2 个答案:

答案 0 :(得分:2)

自定义操作方法选择器类

如果我是你,我会写一个动作方法选择器,并使用它来避免分支你的行动。我写了一个分隔采取可选参数的动作的一个(从而避免了动作中的分支代码 - 简化的单元测试)。默认的Asp.net MVC定义的路由有一个可选的id参数

{controller}/{action}/{id}
id = UrlParameter.Optional

因此有这两种行动方法是有道理的:

public ActionResult Index() { ... }
public ActionResult Index(int id) { ... }

通过编写自定义操作选择器过滤器,我已经完成了。这是一个描述整个事物的detailed blog post,并提供了一些你可以查看的代码。

解决问题的方法

在你的情况下,这意味着你必须编写一个名为SubactionAttribute的自定义动作方法选择器类,然后用它来装饰你的动作:

[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);
}

我不打算为你写全班,但我只会指出你正确的方向,所以你可以按照相同的路径前往你的目标:

[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
}

答案 1 :(得分:0)

您是说有效地想在同一控制器中使用多个“详细信息”操作?如果是这种情况,那么我认为这是开始拆分控制器的一个很好的理由。

您应该从一个名为Members with Actions Details and List(或Index)

的Controller开始
/Members/Details/ID
/Members/List/

你是否有充分的逻辑理由将它们全部放在同一个控制器中?

或者,您可以调用Actions MemberDetails和MemberList以及URL ...

/Controller/MemberDetails/ID
/Controller/MemberList/

给它一个去代码:

[ActionName("Member/Details")]
public ActionResult Members_Details(int id){
    return View();
}