MVC3 and Rewrites

I am writing an MVC3 application which will have to use URL rewriting in the form http: // [server] / [City] - [State] / [some term] /.

As I understand it, MVC3 contains a routing mechanism 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 application), I would use some rewrite URL to decode the url, for example 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 will come from http://www.myserver.com/Home

Is it possible to do this without specifying a controller ... something like this:

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

... , ?

MVC3?

.

+3
3

URL- asp.net MVC3: - url Global.asax: -

       //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" });
+3

You can do this by registering the route in a file Global.asax, but to register a route, it is important that you first register the Old route, then the 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
);
0
source

All Articles