MVC3 and Rewrites

别等时光非礼了梦想. 提交于 2019-12-20 02:06:33

问题


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.


回答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" });



回答2:


Check out these two answers:

  • ASP.NET MVC Routes: How to define custom route
  • Defining custom URL routes in ASP.Net MVC

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.



回答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
);


来源:https://stackoverflow.com/questions/6118840/mvc3-and-rewrites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!