Problem with adding a blazor component to an exsiting Asp.net core project

775 views Asked by At

We have an existing asp.net project one copy is in my system,

Normal structure of a asp.net core project, like below:

-Project
   -- Properties
   -- Areas
   -- Controllers
   -- Views 
   -- ...

One part of code will use blazor serverside but I have faced with a strange issue.

I added the required services in the stratup.cs file:

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
            endpoints.MapBlazorHub();
        });

Next in the root of the project structure I added one file for blazor imports:

_Imports.razor

with the content:

@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop

Then tried to create the blazor component like "~/Components/Pages/Component.razor" After creating the razor file I realized that @page or @code keywords are red underlined as syntax error

I tried to add the namespace in the "~/Views/_ViewImports.cshtml":

@using MyProjectFullName.Components.Pages

Typically, a component's namespace is derived from the app's root namespace and the component's location (folder) within the app. If the app's root namespace is BlazorSample and the Counter component resides in the Pages folder: enter link description here

But I get this error when trying to build the project:

enter image description here

All these are working fine in my system, but when do the same in the other system I get the errors about namespace or @page/@code keywords.

Both visual studio versions are the same -2019- and we are using netcore 3.1.402

I googled a few days but no working solutions could find.

1

There are 1 answers

0
Sajjad.HS On BEST ANSWER

As an alternative solution I decided to move the components to a blazor project and only use the references in the .net core project... It is working without any difference or any problems.

Maybe not the main solution but working for now.

I appreciate any solutions for the main problem