In ASP.NET MVC by default or convention is when we create application, our Views reside in Views directory for our Controller actions. For Example, by default it create Home controller with Index action, and if we see in Solution Explorer in Views directory we can see directory Views, Home, then Index.cshtml and we have its action like the following:
- publicclassHomeController: Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- }
Now by default it will first look for Index.cshtml file in Views/Home folder and if it is unable to find it there then it will find in View/Shared folder. If it do not find there, then an exception will be thrown that view file is not found. Here is the exception text which is thrown:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
See:

The same is the case for partial view when we call return PartialView(), it first looks in the respective controller's Views/Home directory in the case of HomeController and in case of failure it looks in the View/Shared folder.
Now what if we had made a separate directory for partial views in my Views folder and Shared folder like:

Views/Home/Partials and Views/Shared/Partial then we have to tell the ViewEngineto look in that directory as well by writing the following code in Gloabl.asaxfileinApplication_Startevent.
For example, we have this code and we are returning _LoginPartial.cshtml from Index action of HomeController, now what will happen it will look in View/Home directory first and in failure it will look in View/Shared, but this time we have my partial views in separate directory named Partial for every controller and for shared as well, In this case HomeController partial views are in Views/Home/Partials and in Views/Shared/Partials:
- publicclassHomeController: Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- }
- publicclassMvcApplication: System.Web.HttpApplication
- {
- protectedvoidApplication_Start()
- {
- AreaRegistration.RegisterAllAreas();
- WebApiConfig.Register(GlobalConfiguration.Configuration);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- AuthConfig.RegisterAuth();
- RazorViewEnginerazorEngine = ViewEngines.Engines.OfType < RazorViewEngine > ().FirstOrDefault();
- if (razorEngine != null)
- {
- varnewPartialViewFormats = new []
- {
- "~/Views/{1}/Partials/{0}.cshtml",
- "~/Views/Shared/Partials/{0}.cshtml"
- };
- razorEngine.PartialViewLocationFormats = razorEngine.PartialViewLocationFormats.Union(newPartialViewFormats).ToArray();
- }
- }
- }
The same way you can register other directories or your own Custom directory structure if you need to, so doing this way you will not need to specify complete path for like return View("~/Views/Shared/Paritals/Index.cshtml"), instead you can just write then return View() if you want to load Index View and your action name is also Index which is being called, or if you want some other view to be rendered or some other action is invoked and you want to return Index view then you can write return View("Index").

Manas MohapatraPosted Jul 20, 2016, 12:54 AM
Informative one..
Rajeev PunhaniPosted Mar 22, 2016, 4:38 AM
Nice.
Raja TPosted Feb 29, 2016, 1:59 AM
Nice, Thanks for sharing
Gowtham KPosted Feb 28, 2016, 12:31 PM
Good One, Thanks for sharing:)
Ehsan SajjadPosted Jan 11, 2016, 5:20 AM
Thanks for liking post
KaustubhPosted Jan 10, 2016, 9:55 PM
Nice share
Santhakumar MunuswamyPosted Jan 9, 2016, 2:41 AM
Thanks for nice article.
Humayun Kabir MamunPosted Jan 7, 2016, 2:04 AM
Nice...
Debasis SahaPosted Jan 6, 2016, 11:56 PM
Good One..
Upendra Pratap ShahiPosted Jan 6, 2016, 11:55 AM
nice share..