所以我找到了一种不需要任何外部库的方法......
在我的RouteConfig 中,我必须在顶部添加这两条路线,就在IgnoreRoute 下方
routes.MapRoute(
"Root",
"Home/",
new { controller = "Redirect", action = "Home" }
);
routes.MapRoute(
"Index",
"{action}/Index",
new { controller = "Redirect", action = "Home" }
);
然后我必须创建一个名为 Redirect 的新 Controller 并为我的其他每个 Controllers 创建一个方法,如下所示:
public class RedirectController : Controller
{
public ActionResult Home()
{
return RedirectPermanent("~/");
}
public ActionResult News()
{
return RedirectPermanent("~/News/");
}
public ActionResult ContactUs()
{
return RedirectPermanent("~/ContactUs/");
}
// A method for each of my Controllers
}
就是这样,现在我的网站看起来合法了。我的 URL 中不再有主页,不再有索引,这当然有不能接受任何 Index 方法的参数的限制 Controllers 虽然如果真的有必要,你应该能够调整这可以实现你想要的。
仅供参考,如果您想将参数传递给您的索引操作,那么您可以像这样添加第三条路线:
routes.MapRoute(
name: "ContactUs",
url: "ContactUs/{id}/{action}",
defaults: new { controller = "ContactUs", action = "Index", id = UrlParameter.Optional }
);
这将创建一个这样的 URL:/ContactUs/14