简体   繁体   English

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

[英]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.ActionLinkIndexController中的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.

相关问题 如何在MVC C#中从另一个Action方法(都在同一个Controller中)调用一个Action方法? - How to call one Action method from another Action method(both are in the same Controller) in MVC C#? 从一个控制器到另一个控制器调用操作方法 - Call action method from one controller to another 使用Route属性在Controller方法中获取控制器和操作名称 - Get the controller and action name in Controller method with Route attribute 如何通过控制器动作方法注册新路线? - How to register new route from the controller action method? 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller 使用 One From Action 方法覆盖 Controller 上的 ResourceFilter - Overriding ResourceFilter on Controller With One From Action Method 如何从MVC控制器动作调用asyc方法? - How can I call an asyc method from MVC controller action? 如何在ASP.NET MVC中将/ controller / action-name之类的URL路由到controller / action_name - How to route URLs like /controller/action-name to controller/action_name in ASP.NET MVC 如何从 Mvc 中的控制器调用另一个控制器操作 - How to call another controller Action From a controller in Mvc 从一个视图路由到另一个控制器的操作方法,并将参数传递给该控制器的构造函数 - Routing from one view to another controller's action method and passing a parameter to that controller's constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM