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的新手,我想重写我的网址,假设我的网址是www.myhouse.com/product/index/1,那么我只想显示www.myhouse.com/prduct-name为了更好的搜索引擎优化性能,我使用mvc4测试版,我也有一个通过URL Rewriting in .Net MVC,但它不适合我....
但我不知道如何将传递值传递给此方法。
答案 0 :(得分:2)
经过互联网搜索后,我得到了解决方案
将以下代码添加到global.asax
routes.MapRoute(
"Home", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"jats", // Route name
"{jats}", // URL with parameters
new { controller = "Home", action = "Content" } // Parameter defaults
);
然后添加以下代码进行查看:
@Html.ActionLink("test", "Content", new { jats= "test-test" })
将以下代码添加到HomeController
:
public ActionResult Content(string jats)
{
return View();
}
然后你完成了...现在URL与你在查询字符串中传递的相同...所以你的控制器名称和查询字符串参数将不会显示。