0

i want to add two queryString in current request URL if it is not exsist

Edited:

I tried this ---

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.RequestContext.HttpContext.Request.QueryString["mid"] == null && filterContext.RequestContext.HttpContext.Request.QueryString["mod"] == null)
{
mid = Convert.ToString(HttpUtility.ParseQueryString(filterContext.RequestContext.HttpContext.Request.UrlReferrer.Query)["mid"]);
mod = Convert.ToString(HttpUtility.ParseQueryString(filterContext.RequestContext.HttpContext.Request.UrlReferrer.Query)["mod"]);
if (!string.IsNullOrEmpty(mid) || !string.IsNullOrEmpty(mod))
{
RouteValueDictionary redirecttargetDictionary = new RouteValueDictionary();
NameValueCollection Qstr = null;
if (filterContext.HttpContext.Request.RequestType == "GET")
{
Qstr = HttpUtility.ParseQueryString(filterContext.HttpContext.Request.Url.Query);
foreach (string item in Qstr)
{
redirecttargetDictionary.Add(item, Qstr[item]);
}
if (Qstr["mid"] == null)
{
redirecttargetDictionary.Add("mid", mid);
}
if (Qstr["mod"] == null)
{
redirecttargetDictionary.Add("mod", mod);
}
filterContext.Result = new RedirectToRouteResult(redirecttargetDictionary);
}
}
}

This code works fine. -check for quesrystring exsist if no -then take qstring value from 'Urlreferrer' -if request type is GET -then add two querystring with exsisting querystring

But but I having problem if querystring in current request has null value url like http://localhost:53372/question/index?type=&stage.if this ll b the current request url then code ll check for mid n mod qstring as its not exsist it will add both the qstring with exsisting type n stage but value ll b null then before hitting action again OnActionExecuting() get call this time first 'if' is not true and then index method get call but I m not getting type n stage querystring which suppose to null I found url in address bar is http://localhost:53372/question/index?mid=1&mod=5. If I remove filter the I got URL http://localhost:53372/question/index?type=&stage=&mid=1&mod=5 here I m getting both stage n type querystring though its null.

Edited--

[HttpGet]
        public ActionResult Index(ProjectType? projectType, int? projectStageID, int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            IPagedList<ProjectModule> moduleList = null;
            IDictionary<string, string> queryParam = new Dictionary<string, string>();
            queryParam["ProjectType"] = string.Empty;
            queryParam["ProjectStageID"] = string.Empty;
            ViewBag.ProjectType = null;
            ViewBag.ProjectStage = null;
            ViewBag.message = "";
            if ((projectType != null || projectType.HasValue) && projectStageID.HasValue && page.HasValue)
            {

                queryParam["ProjectType"] = Request.QueryString["ProjectType"];
                queryParam["ProjectStageID"] = Request.QueryString["ProjectStageID"];
                //
                //Set View Bag
                //
                ViewBag.ProjectType = Enum.Parse(typeof(ProjectType), Request.QueryString["ProjectType"]);
                ViewBag.ProjectStage = Convert.ToInt32(Request.QueryString["ProjectStageID"]);
                //
                // Set Query String formcollection for Pagging purpose
                //           
                ViewBag.QueryParam = queryParam;
                moduleList = new ProjectModuleService().GetProjectModulesByProjectStageID(Convert.ToInt32(Request.QueryString["ProjectStageID"])).ToPagedList(currentPageIndex, Utility.ConstantHelper.AdminDefaultPageSize); ;
                if (moduleList.Count == 0)
                {
                    ViewBag.message = "Record(s) not found.";
                }
                return View(moduleList);

            }
            else
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ProjectType"]) && !string.IsNullOrEmpty(Request.QueryString["ProjectStageID"]) && !string.Equals(Request.QueryString["ProjectStageID"], "0"))
                {
                    if (!string.IsNullOrEmpty(Request.Params.Get("Index")))
                    {
                        queryParam["ProjectType"] = Request.QueryString["ProjectType"];
                        queryParam["ProjectStageID"] = Request.QueryString["ProjectStageID"];
                        //
                        //Set View Bag
                        //
                        ViewBag.ProjectType = Enum.Parse(typeof(ProjectType), Request.QueryString["ProjectType"]);
                        ViewBag.ProjectStage = Convert.ToInt32(Request.QueryString["ProjectStageID"]);
                        //
                        // Set Query String formcollection for Pagging purpose
                        //           
                        ViewBag.QueryParam = queryParam;
                        moduleList = new ProjectModuleService().GetProjectModulesByProjectStageID(Convert.ToInt32(Request.QueryString["ProjectStageID"])).ToPagedList(currentPageIndex, Utility.ConstantHelper.AdminDefaultPageSize); ;
                        if (moduleList.Count == 0)
                        {
                            ViewBag.message = "Record(s) not found.";
                        }
                        return View(moduleList);
                    }

                    else if (!string.IsNullOrEmpty(Request.Params.Get("Create")))
                    {
                        return RedirectToAction("Create", new { projectStageID = Convert.ToInt32(Request.QueryString["ProjectStageID"]) });
                        //return RedirectToAction("Create", new { projectStageID = Convert.ToInt32(Request.QueryString["ProjectStageID"]), projectType = Enum.Parse(typeof(ProjectType), Request.QueryString["ProjectType"]) });
                    }

                }
                if (Request.QueryString["ProjectType"] == "" || Request.QueryString["ProjectStageID"] == "" || string.Equals(Request.QueryString["ProjectStageID"], "0"))
                {
                    ViewBag.message = "Please select all fields.";
                    return View();
                }
                return View();
            }
        }

What I m doing wrong?

Amogh
  • 4,453
  • 11
  • 45
  • 106
  • 1) Can you please add your action method code here so that i can have some idea about what are you trying to achieve. 2) May i know is this type and stage are same as mid and mod? – alok_dida Nov 01 '12 at 03:56
  • I added code for Action.in that i m checking for QueryString[ProjectType] and [ProjectStageId] (go to last If statement). when i apply above filter to this action,URL is "localhost:53372/question/index?mid=1&mod=5" but if i remove filter URL is "localhost:53372/question/index?type=&stage=&mid=1&mod=5" why after applying filter blank or null querystring is removed.? – Amogh Nov 01 '12 at 04:25
  • @alok_dida any help sir? Added action code. – Amogh Nov 01 '12 at 07:04
  • Am not able to understand, why are you appending mid and mod as a query string if you are not fetching this value in Action method. Second thing, do you have problem with why ProjectType and ProjectStageId coming null or do you have problem with why Mid and Mod are appending? – alok_dida Nov 01 '12 at 09:04
  • Why ProjectType and ProjectStageId coming null? – Amogh Nov 01 '12 at 10:46
  • ProjectType and ProjectStageId coming null, because from the page where you are calling Filter action are not passing values of ProjectType and ProjectStageId. Are you using hidden field for it? Are you using any control? – alok_dida Nov 01 '12 at 11:36
  • I am using @Html.DropDownList for both. and in OnActionExecuting() i add QuickWatch on Qstr which is an NameValueCollection which holds the QueryString From current request it shows both the QeryString but having value as "" (because no value is selected in Dropdown,which seems normal) when execution of filter over it again call OnActionExecuting() at that time i m not getting QueryString.Why OnActionExecuting() get called two times? – Amogh Nov 01 '12 at 13:06

1 Answers1

0

Here what I understand that if there are no querystring in the URL,you need to add two query string parameters. If you are adding this two querystring parameters explicitly than, it will be default value for that queryparameters.

It's better to provide default value in action method instead of adding queryparameters in URL. You can achieve by following code.

public ActionResult ComposeEmail(int queryParameter1 = 0,string queryParameter2 = string.Empty)
{
}

Do let me know, if you have any query.

alok_dida
  • 1,723
  • 2
  • 17
  • 36
  • i want to add using OnActionExecuting(),i want to create an filter which will check for Querystring. – Amogh Oct 31 '12 at 08:54
  • I agreed that you want to create filter functionality. From where you are getting this filter criteria? Are you providing these filters from any page? – alok_dida Oct 31 '12 at 09:26