I know it was possible in Asp.Net to se the default location formats for partial views in Global.asax, and I know that in Asp.Net Core it is possible to set the ViewLocationFormats for views similar to this:
// services is of type IServiceCollection
services.AddMvc(options =>
{
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddRazorOptions(options =>
{
// {0} - Action Name
// {1} - Controller Name
// {2} - Area Name
// Replace normal view entirely
options.ViewLocationFormats.Clear();
options.ViewLocationFormats.Add("/Features/{1}/{0}.cshtml"); //Features/{Controller Name}/{Action Name}.cshtml
options.ViewLocationFormats.Add("/Features/{1}/Views/{0}.cshtml"); //Features/{Controller Name}/Views/{Action Name}.cshtml
options.ViewLocationFormats.Add("/Features/Shared/{0}.cshtml"); //Features/Shared/{Action Name}.cshtml
options.ViewLocationFormats.Add("/Features/Shared/Views/{0}.cshtml"); //Features/Shared/Views/{Action Name}.cshtml
});
For the life of me, I can not figure out how to set the location formats for partial views?
The following portion of code will not work as the Razor engine seems unable to find the partial view:
public async Task<PartialViewResult> EntityChangeDetailModal(EntityChangeListDto entityChangeListDto)
{
var output = await _auditLogAppService.GetEntityPropertyChanges(entityChangeListDto.Id);
var viewModel = new EntityChangeDetailModalViewModel(output, entityChangeListDto);
return PartialView("_EntityChangeDetailModal", viewModel);
}
I know I can explicitly pass in the relative path for the partial view, but I would rather like to be able to just update the default locations so that the Razor engine can resolve these references without me having to make extensive changes to our existing codebase.
For Razor, a partial view is a . cshtml file located under the Views project folder, in a specific controller folder or in the Shared folder. As a developer, you identify partial views by name and can reference them in a Razor view file in either of two ways: using the Html. Partial method or Html.
A partial view is either shared between controllers, than you have to place it in the Shared folder; or it is shared between views of a single controller, than you can place it under controller's views. Please remember, that MVC is using conventions[^].
View search path The default view name for a view component is Default, which means your view file will typically be named Default. cshtml. You can specify a different view name when creating the view component result or when calling the View method. We recommend you name the view file Default.
In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.
Please add this code in startup.cs to configure the default partial views
It is possible to add other locations to the default registered search paths (Pages/Shared and Views/Shared) using Razor view engine options in the ConfigureServices method in Startup. The following code block adds the Pages/Partials folder to the search paths, meaning that you can place partial files there and have them found:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddRazorOptions(options =>
{
options.PageViewLocationFormats.Add("/Pages/Partials/{0}.cshtml");
});
}
customEngine.PartialViewLocationFormats in Global.asax for MVC
ViewEngines.Engines.Clear();
var customEngine = new RazorViewEngine();
customEngine.PartialViewLocationFormats = new string[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Partial/{0}.cshtml",
"~/Views/Partial/{1}/{0}.cshtml"
};
ViewEngines.Engines.Add(customEngine);
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