简体   繁体   中英

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!

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

Just drop the cs file in you App_Code directory

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

@using NameSpace.Authenication

@{
    Authenticated auth = new Authenicated();

 }

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

Create a file named linkRef.cs code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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