majorimi / blazor-components

Components collection and extensions for Blazor applications.
MIT License
327 stars 59 forks source link

.NET 6 compability seems to be broken or I'm doing something wrong #93

Closed munchkindev closed 3 years ago

munchkindev commented 3 years ago

Hello,

So I followed the tutorial on how to implement a modal/banner,

My Startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Majorsoft.Blazor.Components.GdprConsent;

namespace _34digital.ruhr
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = 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)
        {
            //Register dependencies
            services.AddGdprConsent();
            services.AddRazorPages();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}

No errors appeared here. But as soon as I add the code for the banner/modal into _Layout.cshtml or any new shared razor page I get a lot of errors. The most common error type is Name X is not available in current context. Here's a screenshot of the errors marked.

grafik

I am using .NET Core 6 (Blazor) on Visual Studio 2022 Preview 3.1

P.S. yes I'm a beginner at .NET Core and C# in general

majorimi commented 3 years ago

Hi @munchkindev,

Not sure if I get your problem. So do you have any exact build error or not? Your project is running or not?

Some suggestions:

Conclusion is currently .NET 6 is not officially supported but probably all should be ok. Check my suggestions and go through the demo again especially to the @code {} block and _Imports.razor. Right know cannot tell you if it is an issue with the component with .NET 6 or you have incorrect setup.

Let me know if you found the problem or need more help.

munchkindev commented 3 years ago

Hi @majorimi,

Thanks for your extensive and very informational answer.

I might've messed up there - it seems I am using .NET Core 6 and not Blazor? Maybe that's the issue? That's how my hello world app looks like: grafik

Not sure if I get your problem. So do you have any exact build error or not? Your project is running or not?

You understood the problem correctly. The project gets a build error and doesn't run.

is important you can do it globally in _Imports.razor, etc.

It appears there is only a _ViewImports.cshtml file in my project... At least it's the most similar one.

majorimi commented 3 years ago

Ah now I got it.... .cshtm is probably for MVC application. So it is .Net 6 application it is not Blazor! You can use Blazor components in existing MVC applications. But if it's a new app recommend to drop it and create a new Blazor project.

https://www.google.com/url?sa=t&source=web&rct=j&url=https://devblogs.microsoft.com/premier-developer/integrating-blazor-components-into-existing-asp-net-core-mvc-apps/&ved=2ahUKEwjjp-uNv87yAhX1_rsIHT1hAWMQFnoECCAQAQ&usg=AOvVaw1IzUOwFJ_kdnuMFiFx_9V2

Also closing this since this issue is not related to my component.

munchkindev commented 3 years ago

Sorry for reopening this again and sorry for asking this since this has 0 relevance to the component, but the choice against Blazor was that it forces you to develop SPA (Single page applications), which is not good for SEO... Or is this false information?

majorimi commented 3 years ago

Partially true.... When you create a Blazor WASM application (client side) then yes it is true. But Blazor is super flexible it has many hosting models. You can have server side/server hosted Balzor with different render options. Check out render-mode options there is even a "hybrid" where your app renders some HTML to show in the browser (this HTML can have meta tags) and then app will switch to WASM mode.

So Blazor is not a SPA framework as you might think. It is a component based "Web" application framework. And even "WEB" is in quotes since Blazor will be everywhere pretty soon... If not there already... If you write your Blazor components correctly (e.g. no DB or private resource access in the components which will fail on WASM mode) then you can use the SAME components in all apps below:

Even at any point in time you just "port" your application from Server side to WASM or Electron on Mobile with "minimal" effort... Hope this answered your questions.

munchkindev commented 3 years ago

Thank you so much for answering this so in-depth and even though it was completely not related to your component, I really really appreciate it!