路由代码可以在我们项目的Global.asax文件的RegisterRoutes方法下找到。
我在 RegisterRoutes 方法中看到了一个 cookie。
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults);
使用上面的 MapRoute 方法,我们定义了一条新路线。
示例;
public class HaberController : Controller
{
public ActionResult Listele()
{
// Listing codes will be written
return View("Listele");
}
public ActionResult Detay(string HaberId)
{
// Detail codes will be written
return View("Detay");
}
}
我们转到 Global.asax 文件并按如下方式对其进行编辑。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"HaberListeleme",
"Haber",
new { controller = "Haber", action = "Listele" }
);
routes.MapRoute(
"HaberDetay",
"Haber/{id}",
new { controller = "Haber", action = "Detay" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
如果我们按如下方式进行路由:
routes.MapRoute(
"HaberDetay",
"Haber/{*Id}",
new { controller = "Haber", action = "Detay" }
);
所以如果我们在参数名旁边写*,不管News/url标签后面说什么,都会发送到Controller中Detail方法的相关参数。
例如:
http://www.doguhanaydeniz.com/Haber/Turkiye/Guncel/34389
如果请求 URL 作为土耳其 / 当前 / 34389 将作为参数发送。