Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 652893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:19:19+00:00 2026-05-13T22:19:19+00:00

I would like to have 4 actions with the same name (controller methods may

  • 0

I would like to have 4 actions with the same name (controller methods may have a different name, but their ActionName() attribute is the same for all 4 of them:

[ActionName("Same-name")]
public ActionResult AnonAction() { ... }

[HttpPost]
[ActionName("Same-name")]
public ActionResult AnonAction(ModelData data) { ... }

[Authorize]
[ActionName("Same-name")]
public ActionResult AuthAction() { ... }

[HttpPost]
[Authorize]
[ActionName("Same-name")]
public ActionResult AuthAction(OtherData data) { ... }

The first couple does something when users are not authenticated (anonymous users). The second couple does something similar (but not the same) when users are authenticated.

First three action methods work as expected, but I can’t seem to make the last one work. It throws an exception telling me, that it can’t distinguish between POST actions. I don’t think I’ve made anything wrong here or forgotten to do something. I just hope it’s nto a bug in Asp.net MVC 2 RC2.

Does anyone see any flaw in my actions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:19:19+00:00Added an answer on May 13, 2026 at 10:19 pm

    @Paco is right. AuthorizeAttribute doesn’t have anything to do with action selection. His suggestion didn’t feel right so thanks to him I did some digging into MVC code and I came up with the most appropriate solution myself.

    Solution as it was meant to be

    There’s an extensibility point for these things in MVC. Basically what you have to do is to write you own ActionMethodSelectionAttribute that will handle this. I created one that selects action based on user authorization (either anonymous or authorized). Here’s the code:

    /// <summary>
    /// Attribute restricts controller action execution only to either anonymous or authenticated users
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class AllowAuthenticatedAttribute : ActionMethodSelectorAttribute
    {
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="AllowAuthorizedAttribute"/> allows authenticated or anonymous users to execute decorated controller action.
        /// </summary>
        /// <value><c>true</c> if authenticated users are allowed to execute the action; <c>false</c> if anonymous users are allowed to execute the action.</value>
        public bool Authenticated { get; set; }
    
        /// <summary>
        /// Initializes a new instance of the <see cref="AllowAuthorizedAttribute"/> class.
        /// </summary>
        /// <param name="authenticated">If set to <c>true</c> only authorized users will be able to access this action.</param>
        public AllowAuthenticatedAttribute(bool authenticated)
        {
            this.Authenticated = authenticated;
        }
    
        /// <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, System.Reflection.MethodInfo methodInfo)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            return this.Authenticated == controllerContext.HttpContext.User.Identity.IsAuthenticated;
        }
    }
    

    Additional observation

    When I decorated my action methods with my custom attribute I still got the same exception until I added [HttpGet] to my GET actions. Why is that? I found the answer in the flowchart in Pro ASP.NET MVC Framework book (check it out yourself). Exception was thrown because there were more than just one action method with ActionMethodSelectorAttribute. Normally we just decorate out POST actions, but in this case all of them were decorated. 2 for anonymous and 2 for authenticated users. That’s why you have to use both HttpGet and HttpPost on action methods when you add more selector attributes to them.

    My controller actions now look like this

    [HttpGet]
    [AllowAuthenticated(false)]
    [ActionName("Same-name")]
    public ActionResult AnonAction() { ... }
    
    [HttpPost]
    [AllowAuthenticated(false)]
    [ActionName("Same-name")]
    public ActionResult AnonAction(ModelData data) { ... }
    
    [HttpGet]
    [Authorize]
    [AllowAuthenticated(true)]
    [ActionName("Same-name")]
    public ActionResult AuthAction() { ... }
    
    [HttpPost]
    [Authorize]
    [AllowAuthenticated(true)]
    [ActionName("Same-name")]
    public ActionResult AuthAction(OtherData data) { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer item.find('a'); should do it. May 14, 2026 at 9:18 pm
  • Editorial Team
    Editorial Team added an answer When you have this type of problem, Just call the… May 14, 2026 at 9:18 pm
  • Editorial Team
    Editorial Team added an answer Works fine for me. Maybe you just need to throw… May 14, 2026 at 9:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.