![](/img/trans.png)
[英]How to call one Action method from another Action method(both are in the same Controller) in MVC C#?
[英]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.ActionLink
在IndexController
中的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.