13

I modify the default route rule a little bit as below:

routes.MapRoute(
  "Default",                                              // Route name
  "{controller}/{action}/{id}",                           // URL with parameters
  new { controller = "Home", action = "Index", id= (string)null }  // Parameter defaults
);

Then I can set url as:

/Controller/Action/myParam
/Home/Index/MyParam

The default Action Index would be:

public ActionResult Index(string id)
{
  //....
}

I can get the param in action. But I want to get the param in OnActionExecuting. How can I do it?

5 Answers 5

29

You should be able to access it with :

public override void OnActionExecuting(ActionExecutingContext filterContext) {
    string id = filterContext.RouteData.Values["id"];
    //...
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is also exposed via the ActionExecutingContext.ActionParameters property. The ActionParameters property allows you to see or change the parameters that will actually be passed to the action method.
@Levi you should post this as answer
1

It can be accessible from ActionArguments inside OnActionExecuting.

public override void OnActionExecuting(ActionExecutingContext context) {
    string id = context.ActionArguments["id"].ToString();
    //...
}

Comments

0

if you want to get controller, action, and all parameters, you can do this

    var valuesStr = new StringBuilder();
    if (ctx.RouteData != null && ctx.RouteData.Values != null)
        foreach (var v in ctx.RouteData.Values)
            valuesStr.AppendFormat("/{0}", v.Value);
    _logger.Info("executing {0}", valuesStr.ToString());
    which results in the whole path 

results with:

"/Get/Customer/215840"

it should work on multiple parameters just as well.

Comments

0

I use the following code to retrieve and compare the parameters passed to an action (.net core 3.1).

var vals = filterContext.ActionArguments.Values;
var fistobj = vals.FirstOrDefault();
var val = fistobj.GetType().GetProperties().FirstOrDefault(x => string.Equals(x.Name, "nameParameter", StringComparison.OrdinalIgnoreCase)).GetValue(fistobj);
if (val == null || val.ToString() != "value parameter")
{
    filterContext.Result = new JsonResult(ExecuteResult.Fail(JanException.Parameter.API00001));
    //base.OnActionExecuting(filterContext);
    return;
}

More details for OnActionExecuting and a custom Attribute InitializingActionAttribute

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        ControllerActionDescriptor controlActionDescriptor = (ControllerActionDescriptor)filterContext.ActionDescriptor;
        var attributes = controlActionDescriptor.MethodInfo.CustomAttributes;
        if (attributes.Any(a => a.AttributeType == typeof(InitializingActionAttribute)))
        {
            var vals = filterContext.ActionArguments.Values;
            var fistobj = vals.FirstOrDefault();
            var val = fistobj.GetType().GetProperties().FirstOrDefault(x => string.Equals(x.Name, "nameParameter", StringComparison.OrdinalIgnoreCase)).GetValue(fistobj);
            if (val == null || val.ToString() != "value parameter")
            {
                filterContext.Result = new JsonResult(ExecuteResult.Fail(JanException.Parameter.API00001));
                //base.OnActionExecuting(filterContext);
                return;
            }
        }
        base.OnActionExecuting(filterContext);
    }

Comments

-6

From your filterContext you should be able to get whatever you need.


public class MyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
       //Do your stuff here
    }
}

[MyAttribute]
public ActionResult Index(string id)
{
  //....
}

Comments

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.