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

Blazor WASM组件迁移至ASP.NET Core 6 MVC项目后无法渲染的问题

问题:Blazor WASM组件无法在ASP.NET Core 6 MVC视图中渲染

我按照教程创建Blazor WASM项目并集成到ASP.NET Core 6 MVC应用,示例可正常运行,但将相同代码迁移到现有解决方案后,Counter组件无法在.cshtml视图中渲染。我的项目使用IApplicationBuilder而非教程中的WebApplication(虽然后者实现了前者),不确定是否存在其他配置冲突。

Startup类代码

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        SiteConfigurationSettings.LoadConfiguration(Configuration);
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
            options.AddPolicy("AllowSpecific", p => p.WithOrigins(origins)
                .AllowAnyMethod()
                .AllowAnyHeader()));

        services.AddControllersWithViews(options =>
            {
                // provides model binding for json that doesn't bind to a strongly-typed model
                options.ModelBinderProviders.Insert(0, new JsonValueTypeModelBinderProvider());
            })
            .AddSessionStateTempDataProvider()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
            });

        services.Configure<ForwardedHeadersOptions>(options =>
        {
            options.ForwardedHeaders =
                ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
        });

        services.Configure<IISServerOptions>(options =>
        {
            options.AllowSynchronousIO = true;
        });

        services.AddDataProtection()
            .PersistKeysToDbContext<DataProtectionDbContext>()
            .SetApplicationName("FHREC");

        services.AddRazorPages();
        var mvcBuilder = services.AddControllersWithViews();

        #if DEBUG
        mvcBuilder.AddRazorRuntimeCompilation();
        #endif

        // configures file path for static web (js, css) assets
        services.ConfigureOptions<StaticFilePathResolver>();

        services.AddHttpContextAccessor();

        services.RegisterFHServices(new RecRegistrySpec());
        
        services.InitializeAutoMapper();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseHostFiltering();
        
        if (env.IsDevelopment())
        {
            app.UseWebAssemblyDebugging();
        }

        app.UseBlazorFrameworkFiles();
        
        app.UseForwardedHeaders();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
        });

        app.UseSession();

        app.Use(async (context, next) =>
        {
            context.Request.EnableBuffering();
            await next();
        });
        
        app.UseCookiePolicy(
            new CookiePolicyOptions
            {
                Secure = CookieSecurePolicy.Always
            });

        var startupManager = new PortalStartupManager(app);
        app = startupManager.RegisterErrorHandler();

        app.UseHsts();

        app.UseRouting();
        app.UseCors();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                "default",
                "{controller=Login}/{action=Index}/{id?}");

            endpoints.MapRazorPages(); 
            endpoints.MapFallbackToFile("/index.html");
        });

        startupManager.Startup();
    }
}

视图中引用组件的代码

@using FWBlazor.Pages

<component type="typeof(Counter)" render-mode="WebAssemblyPrerendered" />

内容的提问来源于stack exchange,提问作者geoff swartz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 18:44:50