使用RazorGenerator跨项目调用Area时Razor视图布局失效求助
Hey there, let's work through why your Layout isn't loading when calling WebApp's Areas from your Main project using RazorGenerator. This is a common gotcha with cross-project view sharing, so let's break down the likely issues and fixes:
1. Your _ViewStart.cshtml Isn't Being Embedded or Detected
RazorGenerator relies on embedding view files into the target assembly—if your WebApp's _ViewStart.cshtml isn't set up correctly, the Main project won't pick up its Layout configuration.
- Check file properties in WebApp:
- Right-click
_ViewStart.cshtml→ Properties - Set Build Action to
Embedded Resource - Set Custom Tool to
RazorGenerator - Repeat this for your
_Layout.cshtmlinViews/Sharedtoo—both need to be embedded for the Layout chain to work.
- Right-click
2. The Layout Path in _ViewStart Is Relative to WebApp, Not Main
When running from the Main project, the view engine resolves paths relative to Main, not WebApp. So a line like Layout = "~/Views/Shared/_Layout.cshtml"; will look for the Layout in Main's Views folder, not WebApp's.
- Fix the Layout path:
Use the fully qualified embedded resource name (based on your WebApp's namespace and file structure). For example, if your WebApp is namedWebAppand the Layout is inViews/Shared/_Layout.cshtml, update_ViewStart.cshtmlto:
Double-check the namespace matches what RazorGenerator generates—you can verify this by looking at the generated code in WebApp's@{ Layout = "WebApp.Views.Shared._Layout"; }objfolder.
3. Main Project Isn't Configured to Find WebApp's Embedded Views
You need to tell Main's view engine to look for embedded views in the WebApp assembly.
- Add RazorGenerator view engine configuration (ASP.NET MVC example):
In your Main project'sGlobal.asax.cs(orStartup.csif using OWIN), add this code to register the RazorGenerator view engine and include WebApp's assembly:protected void Application_Start() { // ... other startup code // Register RazorGenerator view engine ViewEngines.Engines.Add(new RazorGeneratorMvcViewEngine() { UseEmbeddedViewsAssemblies = true, // Add WebApp's assembly to the list of embedded view sources AdditionalAssemblyNames = new[] { "WebApp" } }); }
4. Missing Static Resources for the Layout
Scaffolded _Layout.cshtml relies on CSS/JS files (like Bootstrap, jQuery) that are probably in WebApp's Content or Scripts folders. If Main doesn't have access to these, the Layout will load but look broken (or like plain HTML).
- Fix static resource access:
- Either copy WebApp's
ContentandScriptsfolders directly into Main's project - Or set WebApp as a dependency of Main, then configure your build to copy these static files to Main's output directory (right-click WebApp → Project References → Set "Copy Local" to true for relevant files, or use a post-build script).
- Either copy WebApp's
After making these changes, clean and rebuild both projects. When you navigate to ~/Test/Default/Index, the Layout should now load correctly—you'll see the scaffolded header, footer, and styles instead of just the plain Index content.
内容的提问来源于stack exchange,提问作者Serene

