0

Basically I am try to add some custom query parameter into my all browser request using MVC action filter.

I am try to add action filter and write below code but getting error. like: NotSupportedException: Collection was of a fixed size.

public class CustomActionFilters : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.RouteData.Values.Keys.Add("customPara");

        filterContext.RouteData.Values.Values.Add("myAllcustomparamter");
                                  //OR
        filterContext.HttpContext.Request.Query.Keys.Add("customPara=myAllcustomparamter"); 
    }
}

So if I am write into url :http://localhost:15556/

than it will be http://localhost:15556?customPara=myAllcustomparamter

if open any other page like http://localhost:15556/image than it will be http://localhost:15556/image?customPara=myAllcustomparamter OR http://localhost:15556/image?inbuildparamter=anyvalue it will be http://localhost:15556/image?inbuildparamter=anyvalue&customPara=myAllcustomparamter

4
  • In this case the action has already executed so I don't think you can add to the route/manipulate the request. What is the goal of the add'l parameter? Are you trying to trigger additional logic? Commented Apr 19, 2019 at 12:57
  • I want to use that custom queries into ARR DISK cache using url rewrite. Commented Apr 20, 2019 at 13:12
  • Can you use the url rewrite in ARR instead? Presumably this is doable prior to disk cache kicking in. Commented Apr 22, 2019 at 15:55
  • I have tried but customPara=myAllcustomparamter this is dynamic and I want to add from code side. once it's added than next step is ARR Commented Apr 24, 2019 at 4:45

1 Answer 1

2

Finally got solution using redirect into action filter.

public class CustomActionFilters : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        string longurl = HttpContext.Current.Request.Url.AbsoluteUri;
        var uriBuilder = new UriBuilder(longurl);
        var query = HttpUtility.ParseQueryString(uriBuilder.Query);
        var myAllcustomparamter = "myAllcustomparamterhere";
        query.Add("customPara", myAllcustomparamter);
        uriBuilder.Query = query.ToString();
        longurl = uriBuilder.ToString();
        if (!filterContext.HttpContext.Request.QueryString.HasValue || (filterContext.HttpContext.Request.QueryString.HasValue && !filterContext.HttpContext.Request.QueryString.Value.Contains("customPara")))
        {
            filterContext.Result = new RedirectResult(longurl);

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

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.