Default.aspx 表单缺失
Default.aspx form missing
我正在努力学习本教程
Tutorial Link
当我使用 ASP.NET Web 窗体应用程序在 VS 2019 ver 16.7.5 Net Framework 4.8 中启动新项目时。我在项目中没有看到 Default.aspx 页面。因为这对我来说看起来像 HTML 页面,所以我尝试将教程中的代码添加到 HTML 容器中。很多错误?
我正在将 C# 代码转换为 VB.
我的问题是我需要添加库吗?
或者如何向项目添加 Default.aspx 表单?
立即解决方案资源管理器
确保您安装了正确的 VS 工作负载和各个组件。
- 打开Visual Studio安装程序
- 点击修改
- 单击工作负载 选项卡
- 确保勾选ASP.NET和web开发,如果没有勾选
- 点击个别组件
- 检查所需的 .NET Framework SDK 和目标包(即:
.NET Framework 4.7.2 SDK
、.NET Framework 4.7.2 targeting pack
、.NET Framework 4.8 SDK
和 .NET Framework 4.8 targeting pack
)
- 如果您做了任何更改,请在右下角select 全部下载,然后安装。然后点击修改.
然后尝试以下操作:
注意:下面的代码由here转换为VB.NET。但是,class 名称已更改。为了进行测试,我使用了 .NET Framework 4.8 版,但其他版本也可以。
VS 2019:
新建项目
打开Visual Studio
单击继续,无需代码
单击文件
Select新建
Select 项目
Select以下:
然后,select:
点击下一步
输入所需的项目名称(例如:RSSFeedReader)
单击创建
Select以下:
可选:在右侧的高级下,取消选中配置 HTTPS
单击创建
打开解决方案资源管理器
- 在 VS 菜单中,单击 查看
- Select 解决方案资源管理器
添加class(名称:RSSFeed.vb)
- 在解决方案资源管理器中,右键单击 <项目名称>(例如:RSSFeedReader)
- Select 添加
- SelectClass...(名称:RSSFeed.vb)
- 单击添加
RSSFeed.vb
Public Class RSSFeed
Public Property Title As String
Public Property Link As String
Public Property PublishDate As String
Public Property Description As String
End Class
添加 WebForm(名称:default.aspx)
- 在解决方案资源管理器中,右键单击 <项目名称>(例如:RSSFeedReader)
- Select 添加
- Select 新项目...
- SelectWeb 表单(名称:default.aspx)
- 单击添加
修改default.aspx
- 在解决方案资源管理器中,右键单击 default.aspx
- Select 查看标记
default.aspx:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="RSSFeedReader._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<h3>Read RSS Feeds</h3>
<form id="Form1" runat="server" >
<!-- Where XYZ refers to the publication from where you wish to fetch the RSS feed from -->
<div style="max-height:350px; overflow:auto">
<asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="5">
<tr>
<td>
<h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
</td>
<td width="200px">
<%#Eval("PublishDate") %>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
<%#Eval("Description") %>
</td>
</tr>
<tr>
<td> </td>
<td align="right">
<a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
修改default.aspx.vb
- 在解决方案资源管理器中,右键单击 default.aspx
- Select 查看代码
default.aspx.vb:
Public Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'ToDo: replace with the URL to your desired RSS feed
Dim rssFeedUrls As List(Of String) = New List(Of String)()
'add desired URLs
rssFeedUrls.Add("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")
rssFeedUrls.Add("http://thehill.com/rss/syndicator/19110")
'get data
PopulateRssFeed(rssFeedUrls)
End Sub
Private Sub PopulateRssFeed(rssFeedUrls As List(Of String))
'create new List
Dim feeds As List(Of RSSFeed) = New List(Of RSSFeed)
Dim currentURL As String = String.Empty
Try
For Each url As String In rssFeedUrls
'set value
currentURL = url
'create new instance
Dim xDoc As XDocument = New XDocument()
'load
xDoc = XDocument.Load(url)
Dim items = From x In xDoc.Descendants("item")
Select New RSSFeed With
{
.Title = x.Element("title").Value,
.Link = x.Element("link").Value,
.PublishDate = x.Element("pubDate").Value,
.Description = x.Element("description").Value
}
If items IsNot Nothing Then
For Each i In items
Dim f As RSSFeed = New RSSFeed() With {
.Title = i.Title,
.Link = i.Link,
.PublishDate = i.PublishDate,
.Description = i.Description
}
'add
feeds.Add(f)
Next
End If
Next
gvRss.DataSource = feeds
gvRss.DataBind()
Catch ex As Exception
'ToDo: replace with desired code
Dim errMsg As String = String.Format("Error (PopulateRssFeed): {0} (url: {1})", ex.Message, currentURL)
Debug.WriteLine(errMsg)
Throw ex
End Try
End Sub
End Class
确保 installed/turned 在 上启用以下 IIS 功能:(Win 7)
打开控制面板
Select 查看方式:小图标
单击程序和功能
在左侧单击 打开或关闭 Windows 功能
展开Internet 信息服务
展开Web 管理工具
检查IIS管理控制台
展开万维网服务
扩展应用程序开发功能
勾选ASP.NET(勾选这个选项也会勾选一些其他选项)
点击确定
更新:
_default
定义在“default.aspx.designer.vb”
打开default.aspx.designer.vb:
- 在解决方案资源管理器中,右键单击 default.aspx
- Select 查看代码生成文件
default.aspx.设计师.vb
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Partial Public Class _default
'''<summary>
'''Form1 control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents Form1 As Global.System.Web.UI.HtmlControls.HtmlForm
'''<summary>
'''gvRss control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents gvRss As Global.System.Web.UI.WebControls.GridView
End Class
更新 2:
创建项目时,如果选择“创建新的 ASP.NET Web 应用程序”:
然后在“Default.aspx”中使用以下代码代替上面的代码:
Default.aspx:
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="RSSFeedReader._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h3>Read RSS Feeds</h3>
<!-- Where XYZ refers to the publication from where you wish to fetch the RSS feed from -->
<div style="max-height:350px; overflow:auto">
<asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="5">
<tr>
<td>
<h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
</td>
<td width="200px">
<%#Eval("PublishDate") %>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
<%#Eval("Description") %>
</td>
</tr>
<tr>
<td> </td>
<td align="right">
<a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Content>
注意:“Default.aspx.vb”的代码相同
更新 3:
根据此 video,如果您想查看 YT RSS 提要,您需要为 URL 使用以下内容:https://www.youtube.com/feeds/videos.xml?channel_id=<channel_id>
注意: 是URL 的最后一部分(即:在最后一个/
之后)。根据视频,有15个条目的限制。
另外,XML 需要使用命名空间。因此,对“default.aspx.vb”使用以下内容:
default.aspx.vb:
Public Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'ToDo: replace with the URL to your desired RSS feed
Dim rssFeedUrls As List(Of String) = New List(Of String)()
'add desired URLs
rssFeedUrls.Add("https://www.youtube.com/feeds/videos.xml?channel_id=UCBB7sYb14uBtk8UqSQYc9-w")
'get data
PopulateRssFeedYT(rssFeedUrls)
End Sub
Private Sub PopulateRssFeedYT(rssFeedUrls As List(Of String))
'create new List
Dim feeds As List(Of RSSFeed) = New List(Of RSSFeed)
Dim currentURL As String = String.Empty
Try
For Each url As String In rssFeedUrls
'set value
currentURL = url
'create new instance
Dim xDoc As XDocument = New XDocument()
'load
xDoc = XDocument.Load(url)
Dim items = From x In xDoc.Descendants("{http://www.w3.org/2005/Atom}entry")
Select New RSSFeed With
{
.Title = x.Element("{http://www.w3.org/2005/Atom}title").Value,
.Link = x.Element("{http://www.w3.org/2005/Atom}link").Attribute("href").Value,
.PublishDate = x.Element("{http://www.w3.org/2005/Atom}published").Value,
.Description = x.Element("{http://search.yahoo.com/mrss/}group").Element("{http://search.yahoo.com/mrss/}description").Value
}
If items IsNot Nothing Then
For Each i In items
Dim f As RSSFeed = New RSSFeed() With {
.Title = i.Title,
.Link = i.Link,
.PublishDate = i.PublishDate
}
If i.Description.Length <= 50 Then
f.Description = i.Description
Else
'only show the first 50 chars
f.Description = i.Description.Substring(0, 50)
End If
'add
feeds.Add(f)
Next
End If
Next
gvRss.DataSource = feeds
gvRss.DataBind()
Catch ex As Exception
'ToDo: replace with desired code
Dim errMsg As String = String.Format("Error (PopulateRssFeed): {0} (url: {1})", ex.Message, currentURL)
Debug.WriteLine(errMsg)
Throw ex
End Try
End Sub
End Class
注意:对于检索所需数据的替代方法,以下可能会有所帮助:
- Get the most viewed videos from a channel and get a channel's featured video using v3
- Get video feeds using Youtube API v3
资源:
- Help using XDocument in LINQ with ASP.Net (VB)
- What Is an RSS Feed? (And Where to Get It)
- Fetch and display RSS feed using ASP.NET
- NASA RSS Feeds
- XDocument.Descendants not returning descendants
我正在努力学习本教程
Tutorial Link
当我使用 ASP.NET Web 窗体应用程序在 VS 2019 ver 16.7.5 Net Framework 4.8 中启动新项目时。我在项目中没有看到 Default.aspx 页面。因为这对我来说看起来像 HTML 页面,所以我尝试将教程中的代码添加到 HTML 容器中。很多错误?
我正在将 C# 代码转换为 VB.
我的问题是我需要添加库吗?
或者如何向项目添加 Default.aspx 表单?
立即解决方案资源管理器
确保您安装了正确的 VS 工作负载和各个组件。
- 打开Visual Studio安装程序
- 点击修改
- 单击工作负载 选项卡
- 确保勾选ASP.NET和web开发,如果没有勾选
- 点击个别组件
- 检查所需的 .NET Framework SDK 和目标包(即:
.NET Framework 4.7.2 SDK
、.NET Framework 4.7.2 targeting pack
、.NET Framework 4.8 SDK
和.NET Framework 4.8 targeting pack
) - 如果您做了任何更改,请在右下角select 全部下载,然后安装。然后点击修改.
然后尝试以下操作:
注意:下面的代码由here转换为VB.NET。但是,class 名称已更改。为了进行测试,我使用了 .NET Framework 4.8 版,但其他版本也可以。
VS 2019:
新建项目
打开Visual Studio
单击继续,无需代码
单击文件
Select新建
Select 项目
Select以下:
然后,select:
点击下一步
输入所需的项目名称(例如:RSSFeedReader)
单击创建
Select以下:
可选:在右侧的高级下,取消选中配置 HTTPS
单击创建
打开解决方案资源管理器
- 在 VS 菜单中,单击 查看
- Select 解决方案资源管理器
添加class(名称:RSSFeed.vb)
- 在解决方案资源管理器中,右键单击 <项目名称>(例如:RSSFeedReader)
- Select 添加
- SelectClass...(名称:RSSFeed.vb)
- 单击添加
RSSFeed.vb
Public Class RSSFeed
Public Property Title As String
Public Property Link As String
Public Property PublishDate As String
Public Property Description As String
End Class
添加 WebForm(名称:default.aspx)
- 在解决方案资源管理器中,右键单击 <项目名称>(例如:RSSFeedReader)
- Select 添加
- Select 新项目...
- SelectWeb 表单(名称:default.aspx)
- 单击添加
修改default.aspx
- 在解决方案资源管理器中,右键单击 default.aspx
- Select 查看标记
default.aspx:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="RSSFeedReader._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<h3>Read RSS Feeds</h3>
<form id="Form1" runat="server" >
<!-- Where XYZ refers to the publication from where you wish to fetch the RSS feed from -->
<div style="max-height:350px; overflow:auto">
<asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="5">
<tr>
<td>
<h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
</td>
<td width="200px">
<%#Eval("PublishDate") %>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
<%#Eval("Description") %>
</td>
</tr>
<tr>
<td> </td>
<td align="right">
<a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
修改default.aspx.vb
- 在解决方案资源管理器中,右键单击 default.aspx
- Select 查看代码
default.aspx.vb:
Public Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'ToDo: replace with the URL to your desired RSS feed
Dim rssFeedUrls As List(Of String) = New List(Of String)()
'add desired URLs
rssFeedUrls.Add("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")
rssFeedUrls.Add("http://thehill.com/rss/syndicator/19110")
'get data
PopulateRssFeed(rssFeedUrls)
End Sub
Private Sub PopulateRssFeed(rssFeedUrls As List(Of String))
'create new List
Dim feeds As List(Of RSSFeed) = New List(Of RSSFeed)
Dim currentURL As String = String.Empty
Try
For Each url As String In rssFeedUrls
'set value
currentURL = url
'create new instance
Dim xDoc As XDocument = New XDocument()
'load
xDoc = XDocument.Load(url)
Dim items = From x In xDoc.Descendants("item")
Select New RSSFeed With
{
.Title = x.Element("title").Value,
.Link = x.Element("link").Value,
.PublishDate = x.Element("pubDate").Value,
.Description = x.Element("description").Value
}
If items IsNot Nothing Then
For Each i In items
Dim f As RSSFeed = New RSSFeed() With {
.Title = i.Title,
.Link = i.Link,
.PublishDate = i.PublishDate,
.Description = i.Description
}
'add
feeds.Add(f)
Next
End If
Next
gvRss.DataSource = feeds
gvRss.DataBind()
Catch ex As Exception
'ToDo: replace with desired code
Dim errMsg As String = String.Format("Error (PopulateRssFeed): {0} (url: {1})", ex.Message, currentURL)
Debug.WriteLine(errMsg)
Throw ex
End Try
End Sub
End Class
确保 installed/turned 在 上启用以下 IIS 功能:(Win 7)
打开控制面板
Select 查看方式:小图标
单击程序和功能
在左侧单击 打开或关闭 Windows 功能
展开Internet 信息服务
展开Web 管理工具
检查IIS管理控制台
展开万维网服务
扩展应用程序开发功能
勾选ASP.NET(勾选这个选项也会勾选一些其他选项)
点击确定
更新:
_default
定义在“default.aspx.designer.vb”
打开default.aspx.designer.vb:
- 在解决方案资源管理器中,右键单击 default.aspx
- Select 查看代码生成文件
default.aspx.设计师.vb
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Partial Public Class _default
'''<summary>
'''Form1 control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents Form1 As Global.System.Web.UI.HtmlControls.HtmlForm
'''<summary>
'''gvRss control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents gvRss As Global.System.Web.UI.WebControls.GridView
End Class
更新 2:
创建项目时,如果选择“创建新的 ASP.NET Web 应用程序”:
然后在“Default.aspx”中使用以下代码代替上面的代码:
Default.aspx:
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="RSSFeedReader._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h3>Read RSS Feeds</h3>
<!-- Where XYZ refers to the publication from where you wish to fetch the RSS feed from -->
<div style="max-height:350px; overflow:auto">
<asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="5">
<tr>
<td>
<h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
</td>
<td width="200px">
<%#Eval("PublishDate") %>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
<%#Eval("Description") %>
</td>
</tr>
<tr>
<td> </td>
<td align="right">
<a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Content>
注意:“Default.aspx.vb”的代码相同
更新 3:
根据此 video,如果您想查看 YT RSS 提要,您需要为 URL 使用以下内容:https://www.youtube.com/feeds/videos.xml?channel_id=<channel_id>
注意:/
之后)。根据视频,有15个条目的限制。
另外,XML 需要使用命名空间。因此,对“default.aspx.vb”使用以下内容:
default.aspx.vb:
Public Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'ToDo: replace with the URL to your desired RSS feed
Dim rssFeedUrls As List(Of String) = New List(Of String)()
'add desired URLs
rssFeedUrls.Add("https://www.youtube.com/feeds/videos.xml?channel_id=UCBB7sYb14uBtk8UqSQYc9-w")
'get data
PopulateRssFeedYT(rssFeedUrls)
End Sub
Private Sub PopulateRssFeedYT(rssFeedUrls As List(Of String))
'create new List
Dim feeds As List(Of RSSFeed) = New List(Of RSSFeed)
Dim currentURL As String = String.Empty
Try
For Each url As String In rssFeedUrls
'set value
currentURL = url
'create new instance
Dim xDoc As XDocument = New XDocument()
'load
xDoc = XDocument.Load(url)
Dim items = From x In xDoc.Descendants("{http://www.w3.org/2005/Atom}entry")
Select New RSSFeed With
{
.Title = x.Element("{http://www.w3.org/2005/Atom}title").Value,
.Link = x.Element("{http://www.w3.org/2005/Atom}link").Attribute("href").Value,
.PublishDate = x.Element("{http://www.w3.org/2005/Atom}published").Value,
.Description = x.Element("{http://search.yahoo.com/mrss/}group").Element("{http://search.yahoo.com/mrss/}description").Value
}
If items IsNot Nothing Then
For Each i In items
Dim f As RSSFeed = New RSSFeed() With {
.Title = i.Title,
.Link = i.Link,
.PublishDate = i.PublishDate
}
If i.Description.Length <= 50 Then
f.Description = i.Description
Else
'only show the first 50 chars
f.Description = i.Description.Substring(0, 50)
End If
'add
feeds.Add(f)
Next
End If
Next
gvRss.DataSource = feeds
gvRss.DataBind()
Catch ex As Exception
'ToDo: replace with desired code
Dim errMsg As String = String.Format("Error (PopulateRssFeed): {0} (url: {1})", ex.Message, currentURL)
Debug.WriteLine(errMsg)
Throw ex
End Try
End Sub
End Class
注意:对于检索所需数据的替代方法,以下可能会有所帮助:
- Get the most viewed videos from a channel and get a channel's featured video using v3
- Get video feeds using Youtube API v3
资源:
- Help using XDocument in LINQ with ASP.Net (VB)
- What Is an RSS Feed? (And Where to Get It)
- Fetch and display RSS feed using ASP.NET
- NASA RSS Feeds
- XDocument.Descendants not returning descendants