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):
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?