0

When I try to add a value in query string collection, I get an error that collection is readonly. Is there anyway of adding a query string from controller class?

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            HttpContext.Request.QueryString.Add("Hello", "World");

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }

1 Answer 1

1

You can't do this directly from the request as it is a request, not a response. Return a redirect instead of the view:

// Create new url
    string url = Request.UrlReferrer.AbsolutePath 
                         + "?" + querystring.ToString();

    return Redirect(url); // redirect

This example returns the requested page, but with a constructed query string.

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.