c# - Razor 页面命名空间与类库冲突
问题描述
我有一个带有命名空间的剃须刀页面,例如MyApp.Pages.MyClass
. 在此页面 cshtml 中,我试图引用类库中的枚举,其命名空间如MyClass.Enums.MyEnum
. 我找不到引用此枚举类型的方法,因为它使用完全限定的命名空间(或尝试添加 using 子句)都导致“类型或命名空间不存在”错误,因为它被MyClass.Enums.MyEnum
视为相对于当前命名空间MyApp.Pages.MyClass
. 有没有一种方法可以在不重命名所涉及的命名空间的情况下引用枚举类型?
解决方案
I have a razor page with a namespace like
MyApp.Pages.MyClass
. Within this page cshtml I am trying to reference an enum in a class library with a namespace likeMyClass.Enums.MyEnum
.result in a "Type or namespace does not exist" error
Can reproduce same issue if we name the page model class as MyClass
, not MyClassModel
.
namespace MyApp
{
public class MyClass : PageModel
In MyClass.cshtml
page and error as below.
To fix it, you can rename your page model class, such as MyClassModel
etc.
You can also try another approach to create and use a nested namespace, like below.
namespace MyApp
{
namespace MyAppNestedNamespace
{
public class MyClass : PageModel
{
public void OnGet()
{
//...
}
//...
}
}
}
The MyClass.cshtml page may look like this:
@page
@model MyAppNestedNamespace.MyClass
@using MyClass.Enums
@{
ViewData["Title"] = "MyClass";
}
<h1>MyClass</h1>
@*your code here*@
推荐阅读
- python-3.x - 使用python从镶木地板文件中提取列名和数据类型
- javascript - 反应渲染道具方法没有被调用
- java - 测试无法运行,编译已结束
- excel - Excel VBA 用于在不使用范围的情况下制作多个图形
- php - php mysql准备好的语句不更新空值
- node.js - 在路由器基础设置上遇到错误 TypeError('Router.use() 需要一个中间件函数但得到一个 ' + gettype(fn))
- c++ - 'std::thread' 如何确定传递给构造函数的可变参数的数量
- line-breaks - 天蓝色数据流的表达式生成器中是否有换行符?
- palantir-foundry - 如何使用数据连接来同步只有模式而没有行的数据集?
- kubernetes - 如何在操作员框架中使用leader-with-lease选举pod成为leader后执行功能?