.NET MVC Routing - Binds 2 routeMap parameters

I need to parse the url as below.

/controller/action/subaction/id

Right now I'm using a sub-action switch to see what actually needs to be done. 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);
        }
    }

This works, but I would prefer to have separate actions for each event directly accessible from the route. If possible, I would like to combine the action and sub-texture in the route map to get access to the correct action.

  • / controller / action / will call action ()
  • / controller / action / subaction will call action_subaction ()
  • / controller / action / subaction / id will call action_subaction (id)

Is this possible directly from the route map?

+5
source share
2 answers

, , . , , ( , - ). Default Asp.net MVC id

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

:

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

, . , , .

, , 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
}
+2

, "" ? , , .

, , Members ( )

/Members/Details/ID
/Members/List/

, ?

MemberDetails MemberList URL ...

/Controller/MemberDetails/ID
/Controller/MemberList/

:

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

All Articles