c# - Razor 语法/WebMatrix - C# 问题

标签 c# .net asp.net razor webmatrix

在 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/

相关文章:

c# - 在 WPF 中强制 App 继承自 System.Windows.Application 的类

.net - 你从混淆程序中得到的 exe 的速度是否不同?

asp.net - IIS/ASP/ASP.net : How to structure web-site to expose mobile version

c# - 查看Json响应码,发现windows phone 8.1的错误

c# - 如何打破这种循环依赖

c# - 如何在异步 ASP.NET Web 服务调用上定义客户端超时?

c# - httpclient PostAsJsonAsync方法未找到.net框架4并行解决方案

c# - .Text 在 .aspx.cs 文件中不起作用

asp.net - Twitter bootstrap + asp.net masterpages,当用户选择它时如何将导航栏项目设置为事件?

c# - 是否可以将文本框作为参数传递?