繁体   English   中英

如何从一个控制器操作方法路由到另一个控制器操作方法而不在MVC 5中显示控制器名称

[英]How to route from one controller action method to another controller action method without displaying controller name in MVC 5

我在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.ActionLinkIndexController中的Index action方法中如下所示

@Html.ActionLink("SUBJECTS", "Subjects","Subjects", new { }, new { @class = "text" })

现在当我点击Index action方法中的SUBJECTS链接时,它应该转到Subjects控制器中的Subjects动作,而不显示URL中的控制器名称。

如何才能做到这一点?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM