AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

View search paths in ASP.NET MVC | Trickcode

By pushpam abhishek
Listen to this article
View search paths in ASP.NET MVC
google

When your action tells the ASP.NET MVC framework to display a view it searches a number of pre-defined locations to find the file to use. It is important for developers to understand the order that the framework searches. If your controller is part of an area then it will start by trying:

~/Areas/AreaName/Views/ControllerName/ViewName.aspx ~/Areas/AreaName/Views/ControllerName/ViewName.ascx ~/Areas/AreaName/Views/Shared/ViewName.aspx ~/Areas/AreaName/Views/Shared/ViewName.ascx

If the controller isn’t part of an area or it didn’t find a file in the locations above then it will try:


~/Views/ControllerName/ViewName.aspx ~/Views/ControllerName/ViewName.ascx ~/Views/Shared/ViewName.aspx ~/Views/Shared/ViewName.ascx

If it still hasn’t found a view file to use then it goes through the same directories again but tries to find files ending with .cshtml or .vbhtml instead of .aspx and .ascx:

~/Areas/AreaName/Views/ControllerName/ViewName.cshtml ~/Areas/AreaName/Views/ControllerName/ViewName.vbhtml ~/Areas/AreaName/Views/Shared/ViewName.cshtml ~/Areas/AreaName/Views/Shared/ViewName.vbhtml ~/Views/ControllerName/ViewName.cshtml ~/Views/ControllerName/ViewName.vbhtml ~/Views/Shared/ViewName.cshtml ~/Views/Shared/ViewName.vbhtml

While the search path is consistently applied you can still end up with strange results if you don’t understand it. Take the example of an application that starts out using the ASPX View Engine. Later you create an area and decide to switch to the Razor View Engine for just that area. In both, you have a controller with the same name and action. You probably expect the area to use the view file ~/Views/Area/AreaName/ControllerName/ActionName.cshtml (or .vbhtml) but it would actually use ~/Views/ControllerName/ActionName.aspx because it’s earlier in the search path. Another common problem caused by the view search paths is when changes to the view don’t appear in the web browser. This is typically caused when the developer has removed a view file from the project but left it on the file system and it is earlier in the search path. To fix this you need to use Windows Explorer to find the files and remove them.

Share this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments