Retrieve Views from Different Folders in MVC
In this post
you will learn how to retrieve the views which is inside another folder. For
example a view which is inside folder Views|Application|General|Index.cshtml. How
will you map it or use it from controller? Is this possible?
Of course! 100%. Today in one the project I got number of view files in one folder ‘Application’ mapped by ‘Application’ controller which is default behavior of MVC runtime. Look at left side of the image which is default location of any view controlled by ‘Application’ controller…right? And in the left side, what I wanted to be arranged?
Achieving this
is very easy. Just modify the controller as given below.
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
controller as given below, it won’t find the matching view.
Now, a question
for you?
What changes I
need to make in controller/or else to map views which is on project root, as
given below.
Reply me
through comments.
Very-very
simple but useful.
Hope this
helps.
i had tried but its not working fine..
ReplyDeleteno, it should work. follow the same steps suggested.
Deletevery Good thanks
ReplyDeleteThanks, really nice and easy solution.
ReplyDeleteHello, nice post.
ReplyDeleteBut how do you solve the problem of the "go to View" to view () located in other folders or sub-folders
1000 Thanks