MVC3 and Rewrites

走远了吗. 提交于 2019-12-01 20:21:54

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" });
Bart Verkoeijen

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.
Shivkumar

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