You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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:

Common Causes & 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.cshtml in Views/Shared too—both need to be embedded for the Layout chain to work.

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 named WebApp and the Layout is in Views/Shared/_Layout.cshtml, update _ViewStart.cshtml to:
    @{
        Layout = "WebApp.Views.Shared._Layout";
    }
    
    Double-check the namespace matches what RazorGenerator generates—you can verify this by looking at the generated code in WebApp's obj folder.

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's Global.asax.cs (or Startup.cs if 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 Content and Scripts folders 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).
Quick Verification Step

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:16:56