繁体   English   中英

Razor语法/ WebMatrix - C#问题

[英]Razor Syntax / WebMatrix - C# Question

在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页面中。

任何帮助表示赞赏,

谢谢!

您导入名称空间,而不是文件。 所以; Authentication在哪个命名空间 例如:

@using My.Utils.Authentication.cs;

还 - 你要放弃; 在剃须刀电话中:

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

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

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

(旁白:你是否故意用相同的值两次调用相同的方法?)

只需将cs文件放在App_Code目录中即可

然后做这样的事情

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

无需添加使用。

此外,如果您想使用.dll,则需要使用

@using NameSpace.Authenication

@{
    Authenticated auth = new Authenicated();

 }

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

创建一个名为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文件夹

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM