首页 > 解决方案 > 如何在具有正确命名空间和目录的情况下修复 C# 中的“类型名称不存在”错误

问题描述

由于相同的错误结果,我在尝试构建项目时遇到问题。此错误指出:

The type 'Shared' (my guess the folder) does not exist in the type 'MyNameSpace'.

https://ibb.co/fDZ0Pyb - 带有错误代码和项目结构的 IDE 图像。这个图片替代品是由于我的低代表不让我发布图片。

我已经尝试寻找解决此问题的答案。我尝试的所有方法(例如:排除文件夹然后将其添加回来,删除 de .vs 文件夹等)均不成功。

在 Blazor 中,_Imports.razor 文件包含应用程序中使用的所有文件。在这个文件中,我添加了以下 using 语句。

@using NeighborHub
@using NeighborHub.Shared
@using NeighborHub.Data

我认为由于该文件夹存在,因此不应弹出此错误。如果我缺少任何东西,请提供帮助。如果这可以被识别为一个错误,我该如何解决这个问题,或者我应该在 github 上打开一个新问题。

POST EDIT https://ibb.co/sqGyw23 - 此图像添加了有关 IDE 和错误的更多详细信息。

除此之外,我正在使用不久前发布的 .NET Core 3.0 preview 9。

导致错误的剃须刀页面如下:

@page "/househubform"
@page "/househubform/{EditId:int}"

@using NeighborHub.Data

@inject Data.ApplicationDbContext db
@inject NavigationManager UriHelper

@attribute [Authorize(Roles = "admin")]

<div class="card">
    <h3 class="card-header text-center">Add a New HouseHub</h3>
    //Some Html code

@*--- Code Below ---*@
@code {
    //Variables used to fill the form
    Data.HouseHub houseHub = new Data.HouseHub();
    private string varSurname { get; set; } = null;
    private string varHouseNumber { get; set; } = null;
    private int varHouseholdSize { get; set; } = 0;
    private int FKId { get; set; } = 0;

    //Drowdown list
    private List<Data.NeighborHub> NeighborHub = new List<Data.NeighborHub>();

    //Error Msg
    public string Errmsg { get; set; }

    //Parameters
    [Parameter]
    public int EditId { get; set; } = 0;

    //On change select list update
    private void SelectListUpdate(ChangeEventArgs Event)
    {
        FKId = Int32.Parse(Event.Value.ToString());
    }

    //Submit form to database
    private async Task SubmitForm()
    {
        //Submit logic and validation
    }

    //Edit form to database
    private async Task EditForm()
    {
        //Edit logic and validation
    }

    //Initializing logic using Async Task
    protected override async Task OnInitializedAsync()
    {
        //If EditId is passed it will start filling the entity to edit
        if (EditId != 0)
        {
            houseHub = await db.DbHouseHubs.FindAsync(EditId);
            //The following will occur only if the entity is found
            if (houseHub != null)
            {
                varSurname = houseHub.Surname;
                varHouseNumber = houseHub.HouseNumber;
                varHouseholdSize = houseHub.HouseholdSize;
                FKId = houseHub.NeighborhubId;
                NeighborHub = db.DbNeighborHubs.ToList();
            }
            //If the entity is not found it will set an error
            else
            {
                Errmsg = "Could not find the entity. Add a new entry or cancel.";
                EditId = 0;
                NeighborHub = db.DbNeighborHubs.ToList();
            }
        }
        //If set to 0 this will create a new entry
        else
        {
            NeighborHub = db.DbNeighborHubs.ToList();
        }
    }
}

标签: c#asp.netblazorblazor-server-side

解决方案


所以我追踪了错误,似乎这可能是一个错误。

在 _Imports.razor 文件中,我有以下内容:

@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Rendering
@using Microsoft.AspNetCore.Components.RenderTree
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@using NeighborHub
@using NeighborHub.Shared
@using NeighborHub.Data //Cause of the error

所以我继续删除

@using NeighborHub.Data

不知何故,这似乎解决了一切。为什么?我完全不确定为什么这个 @using Namespace.Data 语句会导致错误。


推荐阅读