I'm trying to build a filter that when applied pass alongs "utm_" querystring params
This is what I came up with:
public class PassAlongParametersFilter : FilterAttribute, IResultFilter
{
public string Filter { get; set; }
public PassAlongParametersFilter()
{
Filter = "*";
}
public void OnResultExecuting(ResultExecutingContext filterContext)
{
}
public void OnResultExecuted(ResultExecutedContext filterContext)
{
if (filterContext.Result is RedirectToRouteResult)
{
var action = (RedirectToRouteResult) filterContext.Result;
var qs = filterContext.RequestContext.HttpContext.Request.QueryString;
var regex = StringUtils.WildcardToRegex(Filter);
var routeValues = action.RouteValues;
qs.AllKeys.Where(e => Regex.IsMatch(e, regex)).ForEach(s => routeValues[s] = qs[s]);
filterContext.Result = new RedirectToRouteResult(action.RouteName, routeValues, action.Permanent);
}
}
}
I can see the routeValues being populated correctly while debugging but the utm params aren't included in the resulted url.
This is the action with filter:
[HttpPost]
[AllowAnonymous]
[PassAlongParametersFilter(Filter = "utm_*")]
[Route("konto/registrera/externt/{campaignCode}")]
public ActionResult SimpleRegisterExternal(string email, string campaignCode)
{
And this is the action that I try to redirect to:
[AllowAnonymous]
[Route("konto/registrera/tack")]
public ActionResult RegisterThanks(RegisterThanksViewModel model)
{
I'm guessing there's something with the routing?
Ok so I figured it out. Replacing the Result in OnResultExecuted is too late. Here's what I ended up with:
public class PassAlongParametersFilter : ActionFilterAttribute
{
public string Filter { get; set; }
public PassAlongParametersFilter()
{
Filter = "*";
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Result is RedirectToRouteResult)
{
var action = (RedirectToRouteResult)filterContext.Result;
var qs = filterContext.RequestContext.HttpContext.Request.QueryString;
var regex = StringUtils.WildcardToRegex(Filter);
var routeValues = action.RouteValues;
qs.AllKeys.Where(e => Regex.IsMatch(e, regex)).ForEach(s => routeValues[s] = qs[s]);
filterContext.Result = new RedirectToRouteResult(action.RouteName, routeValues, action.Permanent);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.