Retrieve Views From Different Folders in MVC

In this article, you will learn how to retrieve the views in another folder. For example a view that is in the folder "Views|Application|General|Index.cshtml". How will you map it or use it from the controller? Is this possible?

Of course! 100%. Today in one the project I have many view files in one folder "Application" mapped by the "Application" controller which is the default behavior of the MVC runtime. Look at the left side of the image which is the default location of any view controlled by the "Application" controller, right? And in the left side, what do I want to be arranged?

MVC1.jpg


Doing this is very easy. Just modify the controller as in the following.

MVC2.jpg

And it works cool. In the same way you can map rest views, just keep in mind to use "FolderName/ViewName" to map it correctly.

Please note: It works fine at run-time and you will not be able to navigate from the controller as in the following, it won't find the matching view.

MVC3.jpg

Now, a question for you is:

What changes do I need to make in the controller/or else to map views that is on the project root, as in the following.

MVC4.jpg


Similar Articles