C# ASP.NET Web窗体要显示文件目录
我目前正在尝试修改代码,以便创建一个ASP.NET web表单,允许用户查看和下载我所选作品集的信息。但是,当页面加载时,它会被视为我想浏览服务器上承载文件的部分(~/SelectedWorks),由于我的Web.config未配置为允许在该位置或任何位置浏览目录,因此我收到一个错误 下面是页面后面的ASP.NET代码。除了命名不同的类外,页面后面的C#代码与教程中的代码相同。我还专门使用VisualStudio2013 Ultimate(以及它的捆绑版IIS Express)来测试页面。如果有人能帮我弄清楚发生了什么,我将不胜感激C# ASP.NET Web窗体要显示文件目录,c#,asp.net,webforms,directory-browsing,C#,Asp.net,Webforms,Directory Browsing,我目前正在尝试修改代码,以便创建一个ASP.NET web表单,允许用户查看和下载我所选作品集的信息。但是,当页面加载时,它会被视为我想浏览服务器上承载文件的部分(~/SelectedWorks),由于我的Web.config未配置为允许在该位置或任何位置浏览目录,因此我收到一个错误 下面是页面后面的ASP.NET代码。除了命名不同的类外,页面后面的C#代码与教程中的代码相同。我还专门使用VisualStudio2013 Ultimate(以及它的捆绑版IIS Express)来测试页面。如果有
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SelectedWorks.aspx.cs" Inherits="ConflictingGenius_ASP.SelectedWorks" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<section>
<div>
<hgroup>
<h2><%: Page.Title %></h2>
</hgroup>
<asp:ListView ID="productList" runat="server"
DataKeyNames="WorkID" GroupItemCount="4"
ItemType="ConflictingGenius_ASP.Models.SelectedWork" SelectMethod="GetProducts">
<EmptyDataTemplate>
<table >
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td/>
</EmptyItemTemplate>
<GroupTemplate>
<tr id="itemPlaceholderContainer" runat="server">
<td id="itemPlaceholder" runat="server"></td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td runat="server">
<table>
<tr>
<td>
<a href="WorkDetails.aspx?WorkID=<%#:Item.WorkID%>">
<img src="<%#:Item.ImagePath%>"
width="100" height="75" style="border: solid" /></a>
</td>
</tr>
<tr>
<td>
<a href="WorkDetails.aspx?WorkID=<%#:Item.WorkID%>">
<span>
<%#:Item.Title%>
</span>
</a>
<br />
<%-- <span>
<a href="<%#:this.ResolveClientUrl(Item.URLPath)%>">Download</a>
</span>--%>
<br />
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</p>
</td>
</ItemTemplate>
<LayoutTemplate>
<table style="width:100%;">
<tbody>
<tr>
<td>
<table id="groupPlaceholderContainer" runat="server" style="width:100%">
<tr id="groupPlaceholder"></tr>
</table>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr></tr>
</tbody>
</table>
</LayoutTemplate>
</asp:ListView>
</div>
</section>
</asp:Content>
将此代码添加到.cs文件中
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
productList.DataSource = files;
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
string[]filepath=Directory.GetFiles(Server.MapPath(“~/Uploads/”);
列表文件=新列表();
foreach(文件路径中的字符串文件路径)
{
添加(新列表项(Path.GetFileName(filePath),filePath));
}
productList.DataSource=文件;
}
}
在.aspx文件中的ListView中添加此代码
<ItemTemplete>
<asp:LinkButton runat="server" PostBackUrl='<%#Eval("Value") %>' ><%#Eval("Text") %></asp:LinkButton>
</ItemTemplate>
“它被当作……”
-你能解释一下你的意思吗<代码>“我收到一个错误”
-什么错误?你具体是从哪里得到这个错误的?请提供关于代码失败的实际信息,而不仅仅是它的描述。关于我的“它被当作”位,你读过教程链接了吗?应该发生的事情列在那里。此外,错误是这样的:我导航到生成的页面,但我得到的却是:` Web服务器配置为不列出此目录的内容。最可能的原因:•未为请求的URL配置默认文档,并且服务器上未启用目录浏览`