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
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);
}
}
}