Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to get Blazor WASM component rendered in existing ASP.NET Core 6 MVC web app

$
0
0

I was following this tutorial to try and create a new Blazor wasm project and integrate it into an existing ASP.NET Core 6 MVC web app. If I do exactly as the tutorial shows, I can get the example working fine. But as soon as I pull the same code over to my existing solution the Counter component does not render in my .cshtml view.

My startup class is a bit more complicated, so I don't know if that would cause a conflict. For example, in the tutorial, the app is an instance of a WebApplication while in mine, the app is an instance of IApplicationBuilder. Since WebApplication implements IApplicationBuilder, I would think that would be fine. But I'm not sure if something else in the startup might be causing a problem.

Here's my startup class:

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();    }}

And when referencing the FWBlazor project's component in the view.

@using FWBlazor.Pages<component type="typeof(Counter)" render-mode="WebAssemblyPrerendered" />

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>