【发布时间】:2012-07-14 02:12:16
【问题描述】:
Hy
我写了下面的代码
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Controller_Action", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { id = UrlParameter.Optional } // Parameter defaults
);
foreach (var route in GetDefaultRoutes())
{
routes.Add(route);
}
routes.MapRoute(
"UserPage", // Route name
"{id}", // URL with parameters
new { controller = "Home", action = "Get" } // Parameter defaults
);
}
private static IEnumerable<Route> GetDefaultRoutes()
{
//My controllers assembly (can be get also by name)
Assembly assembly = typeof(test1.Controllers.HomeController).Assembly;
// get all the controllers that are public and not abstract
var types = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Controller)) && t.IsPublic && !t.IsAbstract);
// run for each controller type
foreach (var type in types)
{
//Get the controller name - each controller should end with the word Controller
string controller = type.Name.Substring(0, type.Name.IndexOf("Controller"));
// create the default
RouteValueDictionary routeDictionary = new RouteValueDictionary
{
{"controller", controller}, // the controller name
{"action", "index"} // the default method
};
yield return new Route(controller, routeDictionary, new MvcRouteHandler());
}
}
我是 mvc 的新手,我想像这样重写我的 url,假设我的 url 像 www.myhouse.com/product/index/1 那么我只想显示 www.myhouse.com/prduct-name为了获得更好的 seo 性能,我正在使用 mvc4 beta,我也有一个到URL Rewriting in .Net MVC,但它不适合我....
但我不知道如何将值传递给此方法。
【问题讨论】:
-
为什么使用“mvc”(语言无关设计模式的名称)来描述 ASP.NET MVC 4 框架?你是不是也将桌面上的 IE 图标称为“互联网”的人之一?
-
这不是将桌面上的IE图标称为“interent”,在mvc中这可能我不知道这就是我问你的原因......
-
你可以使用url重写(属性重写)请查看我给的链接sreerejith.blogspot.in/2015/07/…
标签: asp.net-mvc asp.net-mvc-routing