我有一个在dot net 2.0中开发的网站,我需要将其更新到MVC框架中。如何在MVC中保持旧URL相同?例如。 WWW.something.com/Home.aspx
和WWW.something.com/About.aspx
。我需要保留这个URL结构。请帮忙。
答案 0 :(得分:0)
使用MVC routing使您的网址与旧网站相同。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "Home.aspx",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "About",
url: "About.aspx",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
请注意,在MVC 5中,您可以使用attribute routing作为将每个路由放入共享RegisterRoutes方法的替代方法。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Important: Put this before your default route
// or attribute routing won't work.
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public class HomeController : Controller
{
[Route("Home.aspx", Name = "Home")]
public ActionResult Index()
{
return View();
}
[Route("About.aspx", Name = "About")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
}
另一种选择是使用新的URL方案并使用IIS rewrite module配置从旧网址到新网址的301重定向,这样您就不会丢失任何流量,所有旧的超链接都会丢失还在工作。