1

Ok so I am trying to build a controller that has the following action methods

public ActionResult ExecuteStep_1a
public ActionResult ExecuteStep_1b
public ActionResult ExecuteStep_2

Etc...

Is there a way to define a route that uses a get parameter concatenated to the action name? So for instance the URL would be /step/ExecuteStep_1a. I tried defining a route with URL equal to:

{controller}/{action}_{number}

with no success. I tried a few other permutations again with no results. If someone could point me in the right direction I'd appreciate it. Oh I set action equal to ExecuteResult_ with the default if that adds to my explanation any.

1 Answer 1

1

You can use root Action and them use reflection like that:

{controller}/{action}/{step}

public ActionResult ExecuteStep(string step){
   try {
      Type thisType = this.GetType();
      MethodInfo theMethod = thisType.GetMethod("ExecuteStep_" + step);
      return theMethod.Invoke(this, null);
   }
   catch {}
}

But there is some speed limitation, if you using Reflection.

Sign up to request clarification or add additional context in comments.

2 Comments

I was trying to avoid having a "root" action. Ultimately, I ended up going with a unique routing record for each action I wanted to map. So I have a url defined as {controller}/1a, {controller}/1b, etc. The defaults put in the controller and corresponding action name, in this case Step and ExecuteStep_1a, ExecuteStep_1b, etc. I decided even if I could use reflection to pull this off, the benefits aren't worth the effort and additional processing.
@Shawn Yeah, this will be smarter choice, if you don't have too many actions (steps), the reflection is there only one of the way how to achieve your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.