【问题标题】:Class1.cs not appearingClass1.cs 没有出现
【发布时间】:2020-10-23 17:38:36
【问题描述】:

在我的 WEB 文件夹中,我有文件 ʻaspx.cs` 和以下代码:

using System;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1.bibliotecario
{
    public partial class adicionar_livros : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\samue\source\repos\WebApplication1\WebApplication1\App_Data\gerenciamentoBiblioteca.mdf;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (con.State == System.Data.ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();

        }

        protected void b1_Click(object sender, EventArgs e)
        {

            string books_image_name = Class1.GetRandomPassword(10);

            string path = "";
            f1.SaveAs(Request.PhysicalApplicationPath + "/bibliotecario/imagens_livros/" + f1.FileName.ToString());
            path = "imagens_livros/"+f1.FileName.ToString();
            
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into livros values('"+ titulolivros.Text +"','"+ path.ToString() + "','"+ nomeautor.Text + "','" + isbn.Text + "','" + qtd.Text + "')";
            cmd.ExecuteNonQuery();

            msg.Style.Add("display", "block");
        }
    }
}

我在 Asp.net 文件夹 App_Code 文件夹中有一个名为 Class1.cs 类。但是,每次我调用 Class1 时,它都无法识别,没有选项可以将其放入我的代码中,我该如何访问它?

这是我的Class1.cs

using System;

namespace WebApplication1.App_Code
{

    public class Class1
    {

        public static string GetRandomPassword(int length)
        {
            char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            string password = string.Empty;
            Random random = new Random();

            for (int i = 0; i < length; i++)
            {
                int x = random.Next(1, chars.Length);
                //For avoiding Repetation of Characters
                if (!password.Contains(chars.GetValue(x).ToString()))
                    password += chars.GetValue(x);
                else
                    i = i - 1;
            }
            return password;
        }

    }
}

【问题讨论】:

  • 您需要使用using 包含您的课程。这回答了你的问题了吗? Import a class in ASP.NET
  • string books_image_name = App_Code.Class1.GetRandomPassword(10);
  • 我怎样才能把它放在我的代码中? using Class1; 在我的apsx 文件中?
  • 当我输入 using WebApplication1.App_Code; 时,据说没有命名空间,当我按照 VDWWD 所说的做时,似乎 App_Code 在当前上下文中不存在
  • 右键单击class1,并确保在属性表中将其设置为编译。

标签: c# asp.net visual-studio


【解决方案1】:

您可以尝试以下步骤访问App_Code文件夹中的课程。

首先,右键单击App_Code文件夹中的.cs文件并检查其属性。

确保“构建操作”设置为“编译”。

像这样:

其次,请在代码中添加命名空间引用。

using WebApplication2.App_Code;

终于,我们可以成功调用代码了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多