在 Windows 窗体中,我可以使用以下代码创建一个名为“Authentication.cs”的类文件:
public class Authentication
{
public string Name;
internal bool Authenticate()
{
bool i = false;
if (Name == "Jason")
{
i = true;
}
return i;
}
}
在 WebMatrix 中,我可以插入一个名为“Authentication.cs”的新类文件,并插入上面的代码。
在我的 default.cshtml 文件中,我这样做:
<body>
@{
Authentication auth = new Authentication();
if(auth.Authenticated("jasonp"))
{
<p>@auth.Authenticated("jasonp");</p>
}
}
</body>
但是不行!它适用于 WinForms 桌面应用程序,但不适用于 WebMatrix。我不知道为什么它不起作用。错误信息是:
"The namespace Authenticate does not exist. Are you sure you have referenced assemblies etc?"
所以,然后在我的 default.cshtml 文件的顶部,我尝试了这个:
@using Authentication.cs;
这导致了完全相同的错误!
我在任何地方都找不到任何文档来告诉您如何将类文件“包含”到您的 WebMatrix 页面中。
感谢任何帮助,
谢谢!
最佳答案
您导入的是命名空间,而不是文件。所以; Authentication
在什么namespace 中?例如:
@using My.Utils.Authentication.cs;
此外 - 你想在 Razor 调用中删除 ;
:
<p>@auth.Authenticated("jasonp")</p>
您还可以在代码中提供完全限定名称:
@{
var auth = new My.Utils.Authentication();
if(auth.Authenticated("jasonp"))
{
<p>@auth.Authenticated("jasonp")</p>
}
}
(旁白:你是故意用相同的值调用同一个方法两次吗?)
关于c# - Razor 语法/WebMatrix - C# 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5338237/