简体   繁体   English

Razor语法/ WebMatrix - C#问题

[英]Razor Syntax / WebMatrix - C# Question

In Windows Forms I can create a class file called 'Authentication.cs' with the following code: 在Windows窗体中,我可以使用以下代码创建名为“Authentication.cs”的类文件:

public class Authentication
{
    public string Name;

    internal bool Authenticate()
    {
        bool i = false;
        if (Name == "Jason")
        {
            i = true;

        }
        return i;
    }
}

In WebMatrix, I can insert a new Class file, called 'Authentication.cs', and insert the above code. 在WebMatrix中,我可以插入一个名为“Authentication.cs”的新类文件,并插入上面的代码。

And in my default.cshtml file, I do this: 在我的default.cshtml文件中,我这样做:

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

But it won't work! 但它不会起作用! It works for the WinForms desktop app, but not in WebMatrix. 它适用于WinForms桌面应用程序,但不适用于WebMatrix。 I don't know why it's not working. 我不知道为什么它不起作用。 The error message is: 错误消息是:

"The namespace Authenticate does not exist. Are you sure you have referenced assemblies etc?" “命名空间Authenticate不存在。你确定你引用了程序集等吗?”

So, then at the top of my default.cshtml file I tried this: 那么,然后在我的default.cshtml文件的顶部我尝试了这个:

@using Authentication.cs;

Which led to the exact same error! 这导致了完全相同的错误!

There's no documentation that I can find anywhere that tells you how to "include" a class file into your WebMatrix pages. 没有任何文档可以在任何地方找到,告诉您如何将类文件“包含”到WebMatrix页面中。

Any help is appreciated, 任何帮助表示赞赏,

Thank you! 谢谢!

You import a namespace, not a file. 您导入名称空间,而不是文件。 So; 所以; what namespace is Authentication in? Authentication在哪个命名空间 For example: 例如:

@using My.Utils.Authentication.cs; @using My.Utils.Authentication.cs;

Also - you want to drop the ; 还 - 你要放弃; in the razor call: 在剃须刀电话中:

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

You can also provide the fully qualified name in the code: 您还可以在代码中提供完全限定的名称:

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

(aside: are you intentionally calling the same method twice with the same values?) (旁白:你是否故意用相同的值两次调用相同的方法?)

Just drop the cs file in you App_Code directory 只需将cs文件放在App_Code目录中即可

then do something like this 然后做这样的事情

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

No need to add a using. 无需添加使用。

Additionally if you wanted to use a .dll then you would need the using 此外,如果您想使用.dll,则需要使用

@using NameSpace.Authenication

@{
    Authenticated auth = new Authenicated();

 }

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

Create a file named linkRef.cs code: 创建一个名为linkRef.cs的文件代码:

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


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

Put it in a folder App_code then by dot net 2012 publish to bin then upload bin folder 把它放在App_code文件夹中然后通过dot net 2012发布到bin然后上传bin文件夹

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

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