【问题标题】:No pages in Classic ASP site will load after entering some code on a single page在单个页面上输入一些代码后,经典 ASP 站点中的任何页面都不会加载
【发布时间】:2012-02-19 20:04:49
【问题描述】:

在我输入一些代码后开始遇到错误之前,此站点运行正常。该代码不在主页上,但现在不会加载任何网站页面。我在 IIS 中重新启动了站点,但没有帮助。

这是我输入的代码:

'Prepare to parse XML
Set objXML = Server.CreateObject(Microsoft.XMLDOM)

'Set Asynchoronous = false
objXML.async = False

'Load the XML file.
'User Server.MapPath method is the XML is located in your site.
'Else you can use the absolute path.
objXML.Load (Server.MapPath(Products.xml))

'If there is any errors pasring the file the notify
If objXML.parseError.errorCode = 0 Then        
    'Response.Write(objXML.parseError.reason)
Else objXML.parseError.errorCode <> 0 Then
    'Get ALL the Elements by the tag name product
    Set products = objXML.getElementsByTagName(product)

    Select Case iItemID
        Case 1
            aParameters = Array(products.item(0).childNodes(0).text, products.item(i).childNodes(2).text, products.item(i).childNodes(2).text)
        Case 2
            aParameters = Array(products.item(1).childNodes(0).text, products.item(i).childNodes(2).text, products.item(i).childNodes(2).text)        
    End Select

    ' Return array containing product info.
    GetItemParameters = aParameters
End If

使用经典 ASP 在 Windows 7 中运行 IIS。使用 Notepad++ 进行编辑。

这是 XML 文件:

<configuration>
<products>
    <product>   
        <image>
            <![CDATA[ /Images/Atlas Gloves.jpg ]]>
        </image>    
        <name>
            <![CDATA[ Atlas Nitrile Touch Gloves ]]>
        </name>
        <description>
            <![CDATA[ Atlas Nitrile Touch is available in 6 vibrant colors, and is America’s #1 glove for the Lawn and Garden market. Atlas gloves have a breathable nylon back and are machine washable. Like a “second skin,” these gloves are the most comfortable! Atlas Nitrile gloves are the #1 gardening gloves. Atlas Nitrile gloves act like a "second skin" between the user and their work, offering full dexterity and grip. Atlas Nitrile Gloves are perfect for gardening, but their uses expand to so many places – the woodshop, the workshop, the workplace. ]]>
        </description>
        <size>
            <![CDATA[ Small, Medium ]]>
        </size>
        <color>
            <![CDATA[ Purple, Pink, Green, Orange ]]>
        </color>
    </product>
</products>
</configuration>

【问题讨论】:

  • 实际的错误信息会有所帮助。
  • 我没有收到错误消息。它只是永远不会加载。
  • 查看你的服务器日志。
  • 我检查了服务器日志,但在此之前它停止了,没有错误消息。我可以看到当天早些时候的错误消息,所以它正在记录,但显然这让它变冷了。最后,我不得不在 IIS 中重新启动 App Pool。我之前已经重新启动了该站点,并且所有内容都显示为正在运行。重新启动应用程序池后,它又恢复了。感谢您的回复。
  • @Darren:你打开服务器端调试了吗?如果是这样,请将其关闭。然后,您可以修复此代码。如果您在问题中添加 products.xml 的示例,有人可能会向您展示您应该如何编写函数。

标签: asp-classic


【解决方案1】:

让我们开始按顺序获取代码:

首先,我们将创建一个小辅助函数,它给定一个父 XML 元素和一个 XPath(可以只是一个子元素的 tagName)将返回一个元素的文本值。在这种情况下,如果找不到元素,我故意选择返回 null,但如果您愿意,可以将返回值留空:

Function GetElemText(parentElem, path)
     Dim elem: Set elem = parentElem.selectSingleNode(path)
     If Not elem Is Nothing Then
         GetElemText = elem.text
     Else
         GetElemText = null
     End If
End Function

现在我们将创建一个小的 VBScript 类,它为每个产品元素提供一个字段。这个类有一个LoadFromXml 方法,给定一个产品xml元素将提取字段值。

Class Product

    Public Image
    Public Name
    Public Description
    Public Size
    Public Color

    Public Sub LoadFromXml(prodElem)
         Image = GetElemText(prodElem, "image")
         Name = GetElemText(prodElem, "name")
         Description = GetElemText(prodElem, "description")
         Size = GetElemText(prodElem, "size")
         Color = GetElemText(prodElem, "color")
    End Sub

End Class

最后我们创建一个GetProduct 函数,给定产品的索引将加载返回一个带有适当产品详细信息的Product 类实例。

Function GetProduct(productIndex)

    Dim objXML: Set objXML = Server.CreateObject("MSXML2.DOMDocument.3.0") 

    objXML.async = False 
    objXML.setProperty "SelectionLanguage", "XPath"
    objXML.Load Server.MapPath("Products.xml") ''# Assumes Products xml in same folder as this script 

    Dim elem: Set elem = objXML.documentElement.selectSingleNode("products/product[" & productIndex & "]") 
    If Not elem Is Nothing Then
        Set GetProduct = new Product
        GetProduct.LoadFromXml elem
    Else
        Set GetProduct = Nothing
    End If

End Function

请注意,使用命名元素消除了对“幻数”的需求,您必须记住这些值或将其放置在常量中,并且非常脆弱。还使用 XPath 作为选择语言和更具体的 ProgID。总而言之更加健壮,在这种情况下也可以工作。

如果您的产品 xml 在应用程序的生命周期内保持相当静态,请考虑以下变化:

Function GetProduct(productIndex)

    Dim objXML
    If IsEmpty(Application.Contents("Products")) Then
        Set objXML = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0") 
        objXML.async = False 
        objXML.setProperty "SelectionLanguage", "XPath"
        Set Application.Contents("Products") = objXML
    Else
        Set objXML = Application.Contents("Products")
    End If

    objXML.Load Server.MapPath("Products.xml") ''# Assumes Products xml in same folder as this script 

    Dim elem: Set elem = objXML.documentElement.selectSingleNode("products/product[" & productIndex & "]") 
    If Not elem Is Nothing Then
        Set GetProduct = new Product
        GetProduct.LoadFromXml elem
    Else
        Set GetProduct = Nothing
    End If

End Function

这会将 XML DOM 加载到应用商店中,从而节省每次需要产品时重新加载的成本。

我建议的另一项更改是,依靠知道产品元素的序数位置来检索它是非常脆弱的。考虑将id="1" 属性添加到产品元素。然后可以通过以下方式检索它:

    Dim elem: Set elem = objXML.documentElement.selectSingleNode("products/product[@id=""" & productIndex & """]")

【讨论】:

  • 非常感谢您的帖子。不幸的是,我要到早上才能检查它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多