[英]How to route from one controller action method to another controller action method without displaying controller name in MVC 5
I have two different MapRoutes in Route.config as follows... 我在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" }
);
and my @HTML.ActionLink
is as follows in Index
action method which is in IndexController
我的
@HTML.ActionLink
在IndexController
中的Index
action方法中如下所示
@Html.ActionLink("SUBJECTS", "Subjects","Subjects", new { }, new { @class = "text" })
Now when I click on SUBJECTS link in Index action method, it should go to Subjects action in Subjects controller with out displaying controller name in the URL. 现在当我点击Index action方法中的SUBJECTS链接时,它应该转到Subjects控制器中的Subjects动作,而不显示URL中的控制器名称。
How can this be done? 如何才能做到这一点?
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 }
);
With the above, calls to 随着上面的呼唤
@Html.ActionLink("SUBJECTS", "Subjects","Subjects", new { }, new { @class = "text" })
should generate 应该生成
/Subjects
The default and more general route will now catch all other requests and route them to the IndexController
which now acts as the site root/index. 现在,默认和更通用的路由将捕获所有其他请求,并将它们路由到
IndexController
,后者现在充当站点根/索引。
for example assuming 例如假设
public class IndexController : Controller {
public ActionResult Index() {...}
public ActionResult ContactUs() {...}
public ActionResult About() {...}
}
the available routes based on above controller are 基于上述控制器的可用路由是
/
/Index
/ContactUs
/About
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.