【问题标题】:Problem with adding a blazor component to an exsiting Asp.net core project将 blazor 组件添加到现有 Asp.net 核心项目的问题
【发布时间】:2023-03-22 12:11:01
【问题描述】:

我们有一个现有的 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

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

我尝试在“~/Views/_ViewImports.cshtml”中添加命名空间:

@using MyProjectFullName.Components.Pages

通常,组件的命名空间源自应用的根命名空间和组件在应用内的位置(文件夹)。如果应用的根命名空间是 BlazorSample,并且 Counter 组件位于 Pages 文件夹中:enter link description here

但在尝试构建项目时出现此错误:

所有这些在我的系统中都可以正常工作,但是当在其他系统中执行相同操作时,我会收到有关命名空间或@page/@code 关键字的错误。

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

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

【问题讨论】:

    标签: asp.net-core blazor-server-side asp.net-blazor


    【解决方案1】:

    作为替代解决方案,我决定将组件移至 blazor 项目并仅使用 .net 核心项目中的引用...它可以正常工作,没有任何区别或任何问题。

    也许不是主要的解决方案,但现在可以工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      相关资源
      最近更新 更多