将 Blazor 组件添加到现有 Asp.net 核心项目时出现问题

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

我们有一个现有的 asp.net 项目,一个副本在我的系统中,

asp.net 核心项目的正常结构,如下所示:

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

一部分代码将使用 blazor 服务器端,但我遇到了一个奇怪的问题。

我在 stratup.cs 文件中添加了所需的服务:

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

接下来,在项目结构的根目录中,我添加了一个用于 blazor 导入的文件:

_Imports.razor

内容:

@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

然后尝试创建类似“~/Components/Pages/Component.razor”的 blazor 组件 创建 razor 文件后,我意识到 @page 或 @code 关键字带有红色下划线作为语法错误

我尝试在“~/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

但我在尝试构建项目时遇到此错误:

所有这些在我的系统中都运行良好,但在其他系统中执行相同操作时,我收到有关命名空间或@page/@code 关键字的错误。

两个 visual studio 版本相同 -2019- 我们使用的是 netcore 3.1.402

我用谷歌搜索了几天,但没有找到可行的解决方案。

作为替代解决方案,我决定将组件移动到 blazor 项目并仅使用 .net 核心项目中的引用...它没有任何差异或任何问题。

也许不是主要的解决方案,但目前有效。

我很欣赏主要问题的任何解决方案