2

In an MVC 5 Web Application, I have an ActionResult with an [HttpGet] annotation. The page works as intended, both by use of Html.BeginForm and by linking to the the page with an Html.ActionLink with parameters. The page loads correctly and the parameters are read and used correctly.

The only tweak I would like to make is have the query string not show in the URL address bar of the browser. I looked for related SO questions, but all I have seen deal with the inverse case.

This is mainly for cosmetic reasons and a matter of curiosity. When posed with the question, "Why would you want to remove the query string?" my answer is, "The query string is needed by the ActionResult to process correctly, but the user doesn't need to see it, and definitely doesn't need a bookmark with the query string."

In my situation, my URL looks like this:

http://localhost:64400/StudentRosters/FilterableIndex?SelectedCampus=PRA&SelectedFiscalYear=FY12

and I want it to look like this (URL without the query string):

http://localhost:64400/StudentRosters/FilterableIndex

I could make the query string disappear by making ActionResult use the [HttpPost]annotation, but I was under the impression that a POST should only be used when you are changing data in the model (e.g., Create or Edit) and that GET should be used when you are only querying data (which I am in this case).

This SO answer from almost four years ago says that not showing the query string in a GET method is not possible without having some kind of in-between methods in place. Has anything changed since then?

In a Microsoft Virtual Academy video on MVC 5, I heard one of the trainers mention that the Route can be used to clean up the URL that the user sees in the browser. He didn't actually show any examples, so I don't know if I misunderstood his statement or not. So far I have not seen an example where the route could be used to hide the query string.

I'm posting my route, just in case it helps.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

I'm coming to the conclusion that if I want to hide the query string, I need to use an [HttpPost] annotation, even though I'm only querying and not changing data. Am I missing anything obvious or simple?

Community
  • 1
  • 1
  • Yes, you need to use `[HttpPost]` (if you do not include the values as query string or route parameters in a GET method, then your method will not get them. –  May 16 '16 at 00:10
  • Yes, I did try an iteration with `[HttpPost]`. Reading more on the topic before I asked this question, I read that Post should only be done when you're changing data in the model, and not querying. I was just under the impression that it was an improper use of that attribute, even though it does give me the URL in the browser without showing the querystring. –  May 16 '16 at 01:03
  • 1
    It important that if your changing data that you use `[HttpPost]` (and not use `[HttpGet]`) but you can still use `[HttpPost]` for querying data. –  May 16 '16 at 01:11
  • I'll stick with that, considering that it does work and I haven't had any problems yet. And, it's a simple tweak in one place. –  May 16 '16 at 01:19

2 Answers2

0

You could add to the ViewBag instead. Or create a model which you can retrieve in your view and send back to the server using @Html.HiddenFor.

garethb
  • 3,951
  • 6
  • 32
  • 52
0

To add to @StevenMueke, I found this document from the W3C (URIs, Addressability, and the use of HTTP GET and POST).

In summary, it states that there are appropriate uses for POST when querying only: to preserve sensitive information, in case the query string is too large, and other reasons.

Use of [HttpPost] in my case so that I can hide the querystring from the address bar is not breaking any coding rules.