0

Using Net.Core 7 I have a few View Components which are located in:

/Pages/Components/Component1/Default.cshtml
/Pages/Components/Component2/Default.cshtml
...

I want to place all views in Components folder and use the Component's name:

/Pages/Components/Component1.cshtml
/Pages/Components/Component2.cshtml
...

I was able to change the name from Default to Component1 using:

public IViewComponentResult Invoke() {

  return View("Component1");

} 

Can this be accomplish with a global configuration?

What about placing all in Components folder and not using one folder per component?

1 Answer 1

0

You can use RazorViewEngineOptions e.g.

builder.Services.AddControllersWithViews();
builder.Services.Configure<RazorViewEngineOptions>(o =>
{
    o.ViewLocationFormats.Clear(); //Or just add another
    o.ViewLocationFormats.Add
("/Components/{0}" + RazorViewEngine.ViewExtension);
   
});

Be careful about o.ViewLocationFormats.Clear() it may make the other mvc controllers not work

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.