GETah
GETah

Reputation: 21419

Default route not capturing query string

I have an Asp.Net MVC application where I want the Home\Index controller action to take an optional parameter which will be used by the server to decide which view to return.

So I defined the following routes:

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

These routes allow me to simplify my url's by not having to specify the Home controller so url's like mysite/Home/logon are simplified to mysite/logon.The Home/Index action where all non resolved urls go looks like this:

public ActionResult Index(string id="")
{
    if (id.Contains("App"))
      return View("AppView")
    return View();
}

PROBLEM

When I send localhost/#app/41 to my server, I expect the id to be #app/41 but it isn't. The id is always set to null.

Is there something wrong/missing with my route definition?

Upvotes: 1

Views: 60

Answers (2)

Saket Kumar
Saket Kumar

Reputation: 4835

# is not an accepted query string character. If you want to pass # as query string parameter, you should encode it to %23. Your correct URL should look something like

localhost/?id=%23app/41

Upvotes: 1

haim770
haim770

Reputation: 49095

You can't use a Fragement identifier as some query-string value because the Fragment Identifier is not being sent to the server at all, it is only used by the Browser (mainly for Anchor links).

If you insist on using #, you'll have to encode it first, but that won't look as pretty as you expect.

Upvotes: 3

Related Questions