Warm tip: This article is reproduced from serverfault.com, please click

Adding sub-directory to “View/Shared” folder in ASP MVC core and calling the view

发布于 2020-02-14 09:38:31

I have some problem with adding subfolder to /Views/Shared in project. Here is my current structure:

/Views/Shared/AreasNavs/{0}.cshtml

From the web I found some solutions:

services.Configure<RazorViewEngineOptions>(o =>
{               
      o.ViewLocationFormats.Add("/Views/Shared/AreasNavs/{0}" + RazorViewEngine.ViewExtension);
});

...and this solution from Adding sub-directory to “View/Shared” folder in ASP.Net MVC and calling the view

services.AddMvc()
    .AddRazorOptions(opt => {
        opt.ViewLocationFormats.Add("/Views/{1}/Partials/{0}.cshtml");
        opt.ViewLocationFormats.Add("/Views/Shared/Partials/{0}.cshtml");
    });

but nothing is work.

Update. this problem is related to the appeal from the Area

Have you any idea?

Thanks!

Questioner
Andrew Vishnyakov
Viewed
0
Jiří Fiala 2020-11-29 01:44:47

I`ve just run into the same problem. The issue is related to area registration. Instead of

services.Configure<RazorViewEngineOptions>(o =>
{               
      o.ViewLocationFormats.Add("/Views/Shared/AreasNavs/{0}" + RazorViewEngine.ViewExtension);
});

use view location for areas

services.Configure<RazorViewEngineOptions>(o =>
{               
      o.AreaViewLocationFormats.Add("/Views/Shared/AreasNavs/{0}" + RazorViewEngine.ViewExtension);
});