3

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.

ElHaix
  • 12,846
  • 27
  • 115
  • 203
  • Do the URLs that will point to your MVC application already exist (links from other apps, PPC, etc.)? Or are you asking about how to capture the parameters? – stepanian May 25 '11 at 02:33
  • I'm asking about properly capture parameters, that are in the form listed above. – ElHaix May 25 '11 at 02:47

3 Answers3

3

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" });
Ram Khumana
  • 844
  • 6
  • 14
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.
Community
  • 1
  • 1
Bart Verkoeijen
  • 16,545
  • 7
  • 52
  • 56
0

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
);
Littm
  • 4,923
  • 4
  • 30
  • 38
Shivkumar
  • 1,903
  • 5
  • 21
  • 32