Calling an Action to Render View located in a Different Top Level Folder - MVC 5

Malam Malam 226 Reputation points
2024-06-18T14:06:09.7233333+00:00

I use something like this if controllers and views are in the same top level folder.

<li>@Html.ActionLink("Classroom", "Index", "MyViewFolder")</li>

How do achieve the same if controllers and views are in different top level folder?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,404 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 50,836 Reputation points
    2024-06-18T14:41:13.81+00:00

    Folders are completely irrelevant in MVC. They have absolutely no impact on how routing works. In fact they give you a false sense of order. For routing purposes the (default) only thing that matters is the controller name and/or the route attributes. If you happen to have 2 controllers with the same name or route attributes, irrelevant of whether they are in different folders, they will collide at runtime. In some apps we don't even follow a "standard MVC" structure but rather break up controllers/views/models by feature area.

    For ActionLink the first value is the textual name. The second value is the action name (normally the method name). The last value, if any, is the name of the controller, minus the -Controller suffix. So ActionLink("Do Something", "List", "My") would generate a link to the controller (in any folder) called MyController's List action.

    Ultimately what happens is the method asks the runtime to generate the URL and the default convention is {controller}/{action}. Note that it does take into account some customizations so it isn't necessarily a one to one mapping. Irrelevant when the URL is clicked then the router finds the correct controller by following the configured rules. Again, folders have absolutely no impact on this.


  2. Lan Huang-MSFT 28,826 Reputation points Microsoft Vendor
    2024-06-19T08:38:28.26+00:00

    Hi @Malam Malam,

    According to the structure of MVC, it is not recommended to add folders manually.

    You need to use Area. We have mentioned the concept of Area in ASP.NET MVC many times in your previous posts.

    Follow the steps in the document to create a new Area,

    https://learn.microsoft.com/en-us/answers/questions/1694428/combine-2-solutions-in-to-1

    and then use the following code in the master page. Pay attention to modifying the parameters in the ActionLink.

    <li>@Html.ActionLink("Link Text", "ActionName", "ControllerName", new { area = "" }, new { @class = "nav-link" })</li>
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments