HOW TO USE FIRST QUERYSTRING PARAMETER AND APPEND TO ACTION IN URL IN ASP.NET MVC 4?

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
 1015  View(s)
Ratings:
Rate this:

I'd like to tidy this url if at all possible.

Curently it looks like this which is returned from an ActionResult GET

http://localhost/Controller/Action?City=SomeCity&GeoLat=00.000&GeoLong=-0.00000494

Here's what I'm trying to achieve

http://localhost/Controller/Action/SomeCity?GeoLat=00.000&GeoLong=-0.00000494

The City parameter isn't used for anything, so manually editing the first url into the second does indeed return the correct data.

I've even tried appending int the City variable to the action, not really ideal.

routes.MapRoute(

                "Default",

                "{controller}/{action}-{City}/",

                new { controller = "House", action = "Location", City = UrlParameter.Optional }

                );

  1. Pravesh Singh

    Post:101

    Points:709
    Re: How to use first querystring parameter and append to action in url in asp.net mvc 4?

    You were almost there with the routing change. Add this code BEFORE the default route

    routes.MapRoute(

        "CityRoute",

        "{controller}/{action}/{City}",

        new { controller = "House", action = "Location" }

    );

    Note that I change the url format slightly and removed the optional parameter part (it's not needed)

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!