Reputation: 503
I'm trying to create a logging handler for a project. I'm using a custom ActionAttribute (LogAction) to read the request to log data. However, I've only been able to grab the Controller and Action values. I've been looking for about an hour and I think the data I need exists somewhere in filterContext.HttpContext.Request.QueryString
but I'm having no luck pulling anything at all from that. It's always empty, or appears to be at the least. I've also looped through all of the RouteData.Values
collection and the only things that ever appear in there is the Controller and the Action values. I've tried logging the whole URL and the RawURL, but the QueryString is nowhere to be found. What am I missing?
Controller:
[LogAction]
public async Task<JsonResult> GetCustomer (int customerId)
{
//do stuff
return Json(json);
}
Attribute:
public class LogActionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var controller = filterContext.RequestContext.RouteData.Values["Controller"]
var action = filterContext.RequestContext.RouteData.Values["Action"];
var parameters = ??;
base.OnActionExecuting(filterContext);
}
}
JS function to call the controller action (AJAX):
self.getCustomer = function () {
var data = { customerId: self.data.viewingCustomerId() };
$.ajax({
url: "/Customers/GetCustomer",
type: "POST",
contentType: "application/json",
async: true,
data: JSON.stringify(data),
success: function (returnedData) {
//do stuff
}
})
}
Upvotes: 0
Views: 110
Reputation: 26075
You can use ActionParameters
on ActionExecutingContext
:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var controller = filterContext.RequestContext.RouteData.Values["Controller"];
var action = filterContext.RequestContext.RouteData.Values["Action"];
var parameters = filterContext.ActionParameters["customerId"]; // access action parameters
base.OnActionExecuting(filterContext);
}
Upvotes: 2