Hide Querystring in MVC action

14,642

Solution 1

no you cannot hide querystring at all. instead of that there is many method
1. session["key"]
2. viewbag
3. $.post in jquery
4.

[HttpPost]
public ActionResult Preview(string Docs)
{
    TempData["Docs"] = Docs;
     return RedirectToAction("UnInvoicedPreview");
}

Solution 2

This is not very good MVC. MVC doesn't really use querystrings, except in special cases.

Suggested Url in MVC would be "/Home/Preview/<docsValue>"

That being said, if you want to hide the parameter, best to use jQuery and do a post to server.

var check="Particular String"
var data = "Docs=" + check;
$.post("/Home/Preview", data);

Note you can still see this value if you look at the view source of the page, but at least it won't show in the Url.

Solution 3

  1. You can store data in session variables or try storing values in cookies.

  2. It would be better to use TempData, which only allows the value to be used once (removed on first access). However, this implies the value will be used almost immediately.

  3. encrypt the querystring.

Solution 4

You can try this:

HttpServerUtility.UrlTokenEncode and HttpServerUtility.UrlTokenDecode to convert byte array to URL-safe string

Share:
14,642
Rahul
Author by

Rahul

Updated on June 04, 2022

Comments

  • Rahul
    Rahul almost 2 years

    I want to hide the Querystring into My controller's action.

    In my application scenariois something like this:

    1) I have opened new action into new Window as:

    var check="Particular String"
    var url = rootUrl("Home/Preview?Docs=" + check);
    window.open(url, '_blank');
    

    2) On controller's Side I have used some code into Controller's action as:

     public ActionResult Preview(string Docs)
            {
                TempData["Docs"] = Docs;
                return RedirectToAction("UnInvoicedPreview");
            }
    

    My Query:: 1) when opening new window it shows the Query string in the Begining(uptill when it doesn't redirects to another action).

    2) I dont want to show that QueryString into the URL.

    3) Is there any way to hide the Query string or can we Encrypt that?

  • Rahul
    Rahul over 10 years
    can I assign value into Session variable as @Session["_UninvoicedDocuments"]=check; Its not working & throws exceptions.
  • Nilesh Gajare
    Nilesh Gajare over 10 years
    @RahulRJ where it is throwing exception in view or in controller while fetching value
  • Rahul
    Rahul over 10 years
    into Jquery of View I am using particular code like :: var check="SomeString"; @Session["_UninvoicedDocuments"]=check;
  • Nilesh Gajare
    Nilesh Gajare over 10 years
    @RahulRJ try setting like this @{Session["_UninvoicedDocuments"] = "check";}
  • Rahul
    Rahul over 10 years
    @Nilesh its not working. And 'check' is a Variable not a string in which I have stored Confidential IDs in concatenated string format, so your answer is not working
  • Erik Philips
    Erik Philips over 10 years
    You may want to remove post, as it won't help for security purposes. (Per new comments)
  • Erik Philips
    Erik Philips over 10 years
    You may want to remove IFrame, as it won't help for security purposes. (Per new comments)
  • Erik Philips
    Erik Philips over 10 years
    You may want to remove post, as it won't help for security purposes. (Per new comments)
  • MaTya
    MaTya over 10 years
    @RahulRJ you use @httpcontext.current.session["key"] in view, but instead of that use @viewbag["key"]
  • Nilesh Gajare
    Nilesh Gajare over 10 years
    @MaTya @httpcontext.current.session["key"] is same as @Session["key"] you dont have to write entire reference and also it is @HttpContext.Current.Session["key"] not @httpcontext.current.session["key"]
  • MaTya
    MaTya over 10 years
    @ Nilesh you canot write @session[""] in view if you not import @using HttpContext; i knw it is same i think RahulRj code not working becoz he has not import it.