Sidarth
Sidarth

Reputation: 136

URL rewriting from a url in querystring to more friendly url

I am facing problems in rewriting my url. As of now my url is in the form of querystring with parameters passed. Now I want to change that url to a more friendly one . Let me give u an example

http://localhost:59423/SomeController/SomeActionMethod?Id=7

to something like

http://localhost:59423/SomeController/SomeActionMethod/7

or something like that.

Now I now I have to make modifications in the route.config file but I am not getting exacly what modifications

I have added something like this

     routes.MapRoute(
                 name: "SomeName",
                 url: "SomeController/SomeActionMethod/{Id}",
                 defaults: new { controller = "SomeController", action ="SomeActionMethod" });

I can access now the view if I type

   http://localhost:59423/SomeController/SomeActionMethod/7

But when i put the url as

http://localhost:59423/SomeController/SomeActionMethod?Id=7

This should automatically change to

http://localhost:59423/SomeController/SomeActionMethod/7

Should It change automatically to the proper url . If not then how to do it? And if yes then what I am missing as it is not getting changed

The action method

         public ActionResult SomeActionMethod(string Id)
    {

        return View((Id));
    }

Upvotes: 0

Views: 517

Answers (2)

Sidarth
Sidarth

Reputation: 136

After many trial and errors I found what I was looking for

As I have already mentioned in my question you can add a map.route in the route.config file

Something like this

routes.MapRoute(
             name: "SomeName",
             url: "SomeController/SomeActionMethod/{Id}",
             defaults: new { controller = "SomeController", action ="SomeActionMethod" });

Or if you have two parameters in the "SomeActionmethod" then

routes.MapRoute(
             name: "SomeName",
             url: "SomeController/SomeActionMethod/{Id}/{parameter2}",
             defaults: new { controller = "SomeController", action ="SomeActionMethod" });

Now you must be having a query string . Something like this

document.location = "/SomeController/SomeActionMethod?Id=" + SomeId + "&parameter2=" + SomeParameter;

Now since you already have a map.route written you can change the above query string to something like this

"/SomeController/SomeActionMethod/" + SomeId + "/" + SomeParameter;

whenever this url is called it will map with the route information you have provided in route.config file as I have given above. And according to that it knows that you have two parameters {Id} and {parameter2} in the "SomeActionMethod" , so it will automatically render the action method and pass the variables.

So you can eliminate the need for having query string in the url and infact have more friendly url which is in terms of slahes "/".

Hope it helps anyone looking out for the same question.

Upvotes: 1

Bangar
Bangar

Reputation: 43

By using IIS URL Rewrite you can configure as you required. URL Rewrite

Upvotes: 0

Related Questions