内部页面的第一个链接工作。后续尝试会导致ASP.net 2.0中出现Http 404错误

时间:2017-05-04 22:35:08

标签: asp.net http http-status-code-404

我在VS 2008中使用ASP.net 2.0,使用Framework 3.5,VB.net代码隐藏

我计划建立一个网站,左边有一个固定的菜单,用固定位置div中的TreeView制作。右边的第二个div是内容。我制作了一个小型测试应用程序,看看这个概念是否适用于主页/内容页面。

我希望菜单在一个地方进行硬编码。 Master页面适用于此。我们的想法是不必随着网站的增长在单独的页面中编辑菜单。

测试应用程序有一个主页面和一个目录页面。这就是我想建立实际网站的方式。 “目录”页面是启动页面。主页面包含菜单项。每个菜单项都链接到Contents.aspx,但附加了一个唯一的查询字符串或/ keywords。内容页面代码隐藏Load事件读取Request.Url.AbsoluteUri。然后,根据附加的querystring或/ keywords,拉取内容数据并将其推送到Contents.aspx页面上的空Literal控件。在这个测试应用程序中,我只是简单地推送纯文本。

这一点很有效,直到我重新点击一个链接。这会导致Http 404错误。我已经尝试在Master和Content Load事件的顶部使用Response.TrySkipIisCustomErrors = True,但没有帮助。我可以多次单击一个菜单链接,页面将继续重新加载。但是在链接之间来回导致错误。

发生错误时,它会在Contents.aspx Load事件开始之前发生。

我希望能够以一种可以在asp.net主机站点上运行的方式解决这个问题。

MasterPage.master:

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" 
Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div  style="position:fixed; top:0px; left:0px; width:300px; height:100%; 
background-color:#ddd; padding-top:10px; padding-left:10px" >
   <a href="Contents.aspx">Home</a><br />
    <a href="Contents.aspx?p=ABCD" >ABCD page</a><br />
    <a href="Contents.aspx/?p=WXYZ" >WXYZ page</a><br />
    </div>
    <div style="position:absolute; top:0px; left:310px; width:40%; 
height:100%; background-color:#eee; padding-top:10px; padding-left:10px" >
        <asp:ContentPlaceHolder id="MainContent" runat="server">

        </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>

Contents.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Contents.aspx.vb" 
Inherits="Contents" MasterPageFile ="~/MasterPage.master" title="Contents"%>
<asp:Content ID="Contents" ContentPlaceHolderID="MainContent" Runat="Server">
   <asp:Literal ID="Contents_Main" runat="server">
   </asp:Literal>
</asp:Content>

Contents.aspx.vb codebehind:

Partial Class Contents
    Inherits System.Web.UI.Page
    Public Sub Contents_Load(ByVal sender As Object, ByVal e As EventArgs) 
Handles Me.Load
        Try
            Dim url As System.Uri = Request.Url
            Select Case True
                Case url.AbsoluteUri.EndsWith("?p=ABCD")
                    Me.Contents_Main.Text = "content for ABCD"
                Case url.AbsoluteUri.EndsWith("?p=WXYZ")
                    Me.Contents_Main.Text = "content for WXYZ"
                Case Else
                    Me.Contents_Main.Text = "Landing Page"
            End Select
        Catch ex As Exception
            Me.Contents_Main.Text = ex.Message & ex.StackTrace
        End Try
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我能够通过在单个aspx表单中使用相同的概念而不是Master / Content来解决这个问题。菜单现在采用单一形式,代码隐藏拉取内容的数据。我不知道是什么导致了Http 404错误,但使用单个aspx表单解决了它。此外,网址必须包含www.example.com?s=values形式的查询字符串,而不是www.example.com/keyword1-keyword2。后一种语法也引发了404错误。