我在Route.config中有两个不同的MapRoutes,如下所示......
routes.MapRoute(
name: "Default",
url: "{action}",
defaults: new { controller = "Index",action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "SubjectRoute",
url: "{action}",
defaults: new { controller = "Subjects", action = "Subjects" }
);
和我的@HTML.ActionLink
在Index
IndexController
操作方法中如下所示
@Html.ActionLink("SUBJECTS", "Subjects","Subjects", new { }, new { @class = "text" })
现在当我点击索引操作方法中的SUBJECTS链接时,它应该转到主题控制器中的主题操作,而不显示URL中的控制器名称。
如何做到这一点?
答案 0 :(得分:0)
routes.MapRoute(
name: "SubjectRoute",
url: "Subjects",
defaults: new { controller = "Subjects", action = "Subjects" }
);
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Index",action = "Index", id = UrlParameter.Optional }
);
通过上述内容,调用
@Html.ActionLink("SUBJECTS", "Subjects","Subjects", new { }, new { @class = "text" })
应生成
/Subjects
默认且更通用的路由现在将捕获所有其他请求并将它们路由到IndexController
,现在它充当站点根目录/索引。
例如假设
public class IndexController : Controller {
public ActionResult Index() {...}
public ActionResult ContactUs() {...}
public ActionResult About() {...}
}
基于上述控制器的可用路线
/
/Index
/ContactUs
/About