3

I have two files called 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.

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. 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.

1

3 Answers 3

3

You cannot use the ActionLink helper to target a specific Controller class.

However you can create a second route definition in your RouteConfig.cs. Let this route point to another namespace. Then put your API code in this namespace:

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 }
);
Sign up to request clarification or add additional context in comments.

Comments

1

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. 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).

Comments

0

Sounds like you are using a standard controller as an api controler. One should be of type Controller, and the other ApiController, then they can both exist with the same name. @Html.ActionLink will only route to Controllers.

public class FilmController : ApiController

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.