2

我是一个 asp.net mvc 新手。自从经典的asp以来,我还没有真正做过任何事情。我想根据用户角色显示来自 Mysql 数据库的文件列表。到目前为止,我已经创建了两个表,其中一些文件可以由多个角色查看。我也在使用 MySql Membership Provider

表格文件

FileID, FileName, FilePath

文件角色

FileID, RoleID

我想我可以将一个文件添加到 files 表中,然后对于每个有权访问该文件的角色,我需要将 fileID 添加到 FilesRole 表中,并RoleIDmy_aspnet_roles表中获取并将其添加到。

然后以某种方式,当有人去查看文件时,它将获取他们所在的每个角色的 ID,然后从数据库中获取文件列表

我不知道从哪里开始,所以任何帮助将不胜感激。

4

2 回答 2

0

让我让你快速了解:

1.创建用户并将其添加到角色

Webdeveloper Express 和 Visual Studio 为您的网站提供了一个 Web 管理工具:

要访问网站管理工具,请在网站菜单上单击 ASP.Net 配置。

参考:http: //msdn.microsoft.com/en-us/library/yy40ytx0.aspx

您可以从那里添加用户和角色以及添加用户到角色。

2. 创建数据库记录(假设您有某种 mysql 管理面板)

现在在您的数据库中创建一些文件记录,并在文件和角色之间创建一些关联记录。我会使用RoleName,而不是RoleID因为默认 RoleProvider 接口适用于string名称。

3.学习如何从ASP.NET查询mysql数据库

例如阅读:http: //support.discountasp.net/KB/a358/how-to-query-a-mysql-database-in-aspnet.aspx

4. 根据用户角色创建正确的查询

string[] userRoles = Roles.GetRolesForUser(HttpContext.Current.User.Identity.Name);

// creates "'role1','role2' from array.
string userRolesForSqlQuery = userRoles.Select(x => "'" + x + "'").Aggregate((a,b) => a + "," + b); 


//Psuedo code for querying the database...
var fileForUserQuery = String.Format("SELECT FileID, FileName, FilePath FROM Files 
                           INNER JOIN FilesRole ON Files.FileID = FilesRole.FileID
                           WHERE FilesRole.RoleName IN ({0});", userRolesForSqlQuery);

希望这能让你到达那里......

于 2012-04-13T10:27:59.980 回答
0

这就是我到目前为止所拥有的。它有效,但我知道这是不正确的,因为我在控制器中做数据库的东西,应该在模型中完成,但我不知道如何以这种方式完成它。

模型

public class files
{
    public int fileid { get; set; }
    public string fileName { get; set; }
    public string filePath { get; set; }
}

控制器

public ActionResult Index()
    {
        IList<files> downloads = new List<files>();
        // Get Users Role(s) thanks Ropstah
        string[] userRoles = Roles.GetRolesForUser(User.Identity.Name);
        string userRolesForSqlQuery = userRoles.Select(x => "'" + x + "'").Aggregate((a, b) => a + "," + b);

        // MySql Connection thanks again Ropstah
        string connectionString = "my data source";
        var MySqlConnectionString = String.Format(@"SELECT fileid, fileName, filePath, FROM downloads
                                                    INNER JOIN downloadsroles on downloads.fileid = downloadsroles.downloadsid
                                                    WHERE downloadsroles.roleName IN ({0});", userRolesForSqlQuery);

        MySqlConnection conn = new MySqlConnection(connectionString);
        MySqlCommand comm = conn.CreateCommand();
        MySqlDataReader Reader;
        comm.CommandText = MySqlConnectionString;
        conn.Open();
        Reader = comm.ExecuteReader();

        while (Reader.Read())
        {
            downloads.Add(new files() { fileName = Reader["fileName"].ToString(),
                                        filePath = Reader["filePath"].ToString()});
        }

        conn.Close();

        return View(downloads);
    }

看法

@foreach (var downloads in ViewData.Model)
{
<tr>
    <td>
        @downloads.fileName
    </td>
    <td>
        @downloads.filePath
    </td>
</tr>
}
于 2012-04-19T05:40:09.470 回答