3

在 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。我不知道为什么它不起作用。错误信息是:

“命名空间 Authenticate 不存在。你确定你引用了程序集等吗?”

所以,然后在我的 default.cshtml 文件的顶部,我尝试了这个:

@using Authentication.cs;

这导致了完全相同的错误!

我在任何地方都找不到任何文档来告诉您如何将类文件“包含”到您的 WebMatrix 页面中。

任何帮助表示赞赏,

谢谢!

4

3 回答 3

2

您导入命名空间,而不是文件。所以; 什么命名空间Authentication?例如:

@使用 My.Utils.Authentication.cs;

另外 - 你想放弃;剃刀呼叫:

<p>@auth.Authenticated("jasonp")</p>

您还可以在代码中提供完全限定名称:

   @{
      var auth = new My.Utils.Authentication();
      if(auth.Authenticated("jasonp"))
      {
         <p>@auth.Authenticated("jasonp")</p>
      }
   }

(除此之外:您是否故意使用相同的值两次调用相同的方法?)

于 2011-03-17T11:21:18.340 回答
1

只需将 cs 文件放到您的 App_Code 目录中

然后做这样的事情

    @{
      Authentication auth = new Authentication();
      if(auth.Authenticated("jasonp"))
      {
         <p>@auth.Authenticated("jasonp");</p>
      }
   }

无需添加使用。

此外,如果您想使用 .dll,那么您将需要 using

@using NameSpace.Authenication

@{
    Authenticated auth = new Authenicated();

 }

 @if(@auth.Authenticated("jasonp"))
 {
    <p>@auth.Authenticated("jasonp")</p>
 }
于 2011-03-17T17:55:54.477 回答
0

创建一个名为 linkRef.cs 的文件代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class linkRef
{
   public  linkRef() {
        //
        // TODO: Add constructor logic here   
    //
   }
}

把它放在一个文件夹 App_code 然后通过 dot net 2012 发布到 bin 然后上传 bin 文件夹

于 2013-01-28T18:14:54.620 回答