MVC3 and Rewrites

I’m writing an MVC3 application that will need to make use of URL rewriting in the form of http://[server]/[City]-[State]/[some term]/ .

As I understand it, MVC3 contains a routing engine that uses {controler}/{action}/{id} which is defined in the Global.asax file:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

Traditionally (in a non-MVC app), I would use some URL rewriting flavor to decode a url such as http://www.myserver.com/City-State/somesearch/ to querystring parameters that look something like this:
http://www.myserver.com/city=City&state=State&query=somesearch

Keep in mind that this request would be coming from http://www.myserver.com/Home

Can this can be accomplished without having to specify a controller… something like this:

routes.MapRoute(
            "Results",
            "{city}-{state}/{searchTerm}",
            new { controller = "Results", action = "Search" }
        );

… or is it really best to have the controller listed?

How do you handle this in an MVC3 environment?

Thanks.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

URL rewriting in asp.net MVC3:-
you can write code for url rewriting in Global.asax file :-

       //Default url
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",
            "", 
            new { controller = "Home", action = "Index", id = "" }
        );

      //others url rewriting you want

        RouteTable.Routes.MapRoute(null, "Search/{City_State}/{ID}", new { controller = "Home", action = "Search" });

Method 2

Check out these two answers:

Summary:

  • Specify custom routes before the default one.
  • Define specific routes before general as they may match both.
  • Default values are optional.
  • Specify default Controller and Action in the default parameter object.

Method 3

You can do this by registering route in Global.asax file, but order to register the Route is important you must be register first Old route then new one.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// for Old url 
routes.MapRoute(
    "Results",
    "{city}-{state}/{searchTerm}",
    new { controller = "Results", action = "Search" }
);

// For Default Url 
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x