Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the query strings in my ASP.NET MVC route?

On an ASP.NET MVC (Beta) site that I am developing sometimes calls to ActionLink will return to me URLs containing querying strings. I have isolated the circumstances that produce this behavior, but I still do not understand why, instead of producing a clean URL, it decides to using a query string parameter. I know that functionally they are the same, but for consistency (and appearance) of the URLs this is not what I want.

Here are my routes:

routes.MapRoute(
    "Photo Gallery Shortcut",
    "group/{groupname}",
    new { controller = "Photos", action = "All", Id = "" });

routes.MapRoute(
    "Tagged Photos", //since the Tagged action takes an extra parameter, put it first
    "group/{groupname}/Photos/Tagged/{tagname}/{sortby}",
    new { controller = "Photos", action = "Tagged", Id = "", SortBy = "" });

routes.MapRoute(
    "Photo Gallery", //since the Gallery's defualt action is "All" not "Index" its listed seperatly
    "group/{groupname}/Photos/{action}/{sortby}",
    new { controller = "Photos", action = "All", Id = "", SortBy = "" });

routes.MapRoute(
    "Group",  //<-- "Group" Category defined above
    "group/{groupname}/{controller}/{action}/{id}",
    new {controller = "Photos", action = "Index", Id = ""});

Now the problem only occurs when I am looking at the view described by the route named "Tagged Photos" and execute ActionLink via:

Html.ActionLink<PhotosController>(p => p.All((string)ViewData["group"], ""), "Home")

Which produces the URL:

http://domain/group/GROUPNAME?sortBy=

From any other view the URL produced is:

http://domain/group/GROUPNAME

I have pulled down Phil's ASP.NET Routing Debugger, and everything appears in order. This one has me stumped. Any ideas?

like image 874
Jason Whitehorn Avatar asked Oct 22 '08 04:10

Jason Whitehorn


People also ask

What is query string in MVC?

Generally, the query string is one of client-side state management techniques in ASP.NET in which query string stores values in URL that are visible to Users. We mostly use query strings to pass data from one page to another page in asp.net mvc. In asp.net mvc routing has support for query strings in RouteConfig.

How can hide query string in URL in ASP.NET MVC?

You cannot hide this parameter. Use POST instead of GET calls to remove parameters from url. You will still be able to see the parameter in the request message. The only way to safely hide the parameter is to encrypt it.

Why do we need query string?

A query string is the portion of a URL where data is passed to a web application and/or back-end database. The reason we need query strings is that the HTTP protocol is stateless by design. For a website to be anything more than a brochure, you need to maintain state (store data).


1 Answers

Not sure why different views are producing different URLs.

But you can get rid of that sortBy param by assigning a default value to the first route.

new { sortBy = "" }

During generation, if sortBy matches the default, the route engine will skip that parameter (if it's in the query string).

like image 171
CVertex Avatar answered Nov 10 '22 00:11

CVertex