I am having a problem setting the 'Search Path' for my Partial Pages (Views) in my .NET Core 3.1 app. When I place the _SomePartialPagePartial.cshtml in the default search path directories (/Pages/Shared/ or /Views/Shared/), or in the same directory as the calling Razor page, the AJAX script calls the public async Task<PartialViewResult> OnPostSomeMethodPartial(string ID, string ID2, ...) and returns the view with return Partial("_SomePartialPagePartial", SomeDataModel) as expected.
But, when I place the _SomePartialPagePartial.cshtml file in another directory, the Razor page cannot find it.
The reason I want to place the Partial Page in another directory is for organizational purposes. I am using Areas to organize the project and would like to keep the partial pages in certain directories. For example, my structure is:
Areas
Area1
Models
Pages
Partials
Services
Area2
Models
Pages
Partials
Services
etc.
I would like to add a 'Partials' directory to certain Areas (i.e. Areas/Area2/Partials) and place the related partial pages there. The complexity arises when I try to reference a partial page in Areas/Area2/Partials from a Razor Page in Areas/Area1/Pages. In this case, the Razor page CANNOT find the partial file.
I already tried appending the search path in the Startup.cs file (as described in https://www.learnrazorpages.com/razor-pages/partial-pages) but that doesn't seem to work?? The options.PageViewLocationFormats path added via services.Configure<RazorViewEngineOptions>(options => ... does NOT append the path search array as verified in the debugger.
Perhaps the best solution would be to allow setting the directory of the partial file directly in the call (i.e. Partial("<optional path>/_SomePartialPagePartial", SomeDataModel) or Partial("_SomePartialPagePartial", <optional path>, SomeDataModel)). An enhancement?
Anyway, any help/suggestions on how to resolve this would be greatly appreciated... :)
P.S. I am using the latest/updated version of VS 2019 (Version 16.11.3).
I struggled with this issue on .NET 6 when trying to include a partial view within a razor page with customized location formats.
I found answers that suggest deriving the RazorViewEngine and customizing the PartialViewLocationFormats, which in .NET 6 isn't applicable since no such property exists anymore.
Yet, it turned out to have a different approach. If you navigate to the definition of the RazorViewEngineOptions and take a look at the comments, you'll find out that the one describing PageViewLocationFormats says:
Gets the locations where
RazorViewEnginewill search for views (such as layouts and partials) when searched from the context of rendering a Razor Page.
So, to match the following configuration pattern:
options.ViewLocationFormats.Clear();
options.ViewLocationFormats.Add("/Gui/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
options.ViewLocationFormats.Add("/Gui/Views/Shared/{0}" + RazorViewEngine.ViewExtension);
options.AreaViewLocationFormats.Add("/Gui/Areas/{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
options.AreaViewLocationFormats.Add("/Gui/Areas/{2}/Shared/{0}" + RazorViewEngine.ViewExtension);
I added one more line,
options.PageViewLocationFormats.Add("/Gui/Views/Shared/{0}" + RazorViewEngine.ViewExtension);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With