Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Syntax / WebMatrix - C# Question

In Windows Forms I can create a class file called 'Authentication.cs' with the following code:

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.

And in my default.cshtml file, I do this:

<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. 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?"

So, then at the top of my default.cshtml file I tried this:

@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.

Any help is appreciated,

Thank you!


1 Answers

You import a namespace, not a file. So; what namespace is Authentication in? For example:

@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?)

like image 198
Marc Gravell Avatar answered May 17 '26 06:05

Marc Gravell