简体   繁体   English

ASP.NET MVC5,两个控制器,不同文件夹同名

[英]ASP.NET MVC5, two controller, with the same name in different folders

I have two files called FilmController.我有两个名为 FilmController 的文件。 One in the controller folder which displays my database data, and one in a folder called API which allows users to view data in json format.一个在显示我的数据库数据的控制器文件夹中,一个在名为 API 的文件夹中,它允许用户以 json 格式查看数据。

My question is, in my nav bar i have an action link item that linked to the controller folder film file before i created the API one.我的问题是,在我创建 API 之前,在我的导航栏中,我有一个链接到控制器文件夹电影文件的操作链接项。 Now it doesn't no which one to target.现在不是没有目标。 Is there anyway to target a specific one.反正有没有针对特定的。

<li>@Html.ActionLink("Films", "Index", "Film")</li>

I want this to direct to the controller/film file.我希望它指向控制器/电影文件。

You cannot use the ActionLink helper to target a specific Controller class.您不能使用 ActionLink 帮助程序来定位特定的 Controller 类。

However you can create a second route definition in your RouteConfig.cs.但是,您可以在 RouteConfig.cs 中创建第二个路由定义。 Let this route point to another namespace.让这个路由指向另一个命名空间。 Then put your API code in this namespace:然后将您的 API 代码放在此命名空间中:

routes.MapRoute(
    "API",
    "api/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { "MyMvcApp.Api" }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

For those who have similar issue having two controllers with the same name in different folder, consider using Areas in your ASP NET MVC project and put each of these controllers to a different area.对于那些在不同文件夹中有两个同名控制器有类似问题的人,请考虑在 ASP NET MVC 项目中使用区域并将这些控制器中的每一个放在不同的区域。 Then if you use @Html.ActionLink("Name", "Action", "Controller") in your view it will always choose the controller based on the area you are in, and if you want to have a link to a controller from another area, you can use @Html.ActionLink("Name", "Action", "Controller", new { area = "AreaName" }, null) .然后,如果您在视图中使用@Html.ActionLink("Name", "Action", "Controller") ,它将始终根据您所在的区域选择控制器,并且如果您想从另一个区域,您可以使用@Html.ActionLink("Name", "Action", "Controller", new { area = "AreaName" }, null)

Sounds like you are using a standard controller as an api controler.听起来您正在使用标准控制器作为 api 控制器。 One should be of type Controller, and the other ApiController, then they can both exist with the same name.一个应该是 Controller 类型,另一个是 ApiController,那么它们可以以相同的名称存在。 @Html.ActionLink will only route to Controllers. @Html.ActionLink 只会路由到控制器。

public class FilmController : ApiController

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

相关问题 身份:在两个不同的asp.net mvc5应用程序中的身份验证 - Identity: Authentication in Two Different asp.net mvc5 Applications ASP.NET MVC5控制器和Autofac - ASP.NET MVC5 controller and Autofac 从同一服务器上的ASP.NET MVC5控制器调用经典ASP - Calling Classic ASP from ASP.NET MVC5 Controller on the same server 在ASP.net 5 MVC 6中,如何在不同的命名空间中使用相同的控制器名称 - In ASP.net 5 MVC 6 , How to use same controller name in different namespaces ASP.Net MVC5 HTML.ActionLink到不同控制器中的索引方法 - ASP.Net MVC5 Html.ActionLink to index method in different controller 具有相同控制器名称(不同名称空间)的ASP.net MVC单独解决方案 - ASP.net MVC seperate solutions with same controller name (different namespace) 在同一控制器中的两个ActionResult之间传递数据,从而在ASP.NET MVC中呈现不同的视图 - Passing data between two ActionResults in the same controller rendering different views in asp.net mvc 具有相同名称的ASP.NET MVC Controller操作 - ASP.NET MVC Controller actions with the same name 与区域同名的控制器 - Asp.Net MVC4 - Controller with same name as an area - Asp.Net MVC4 变量在ASP.NET MVC5中的控制器方法中不起作用 - Variable does not work in controller method in asp.net mvc5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM