【问题标题】:Get current root path url of the page获取页面当前根路径url
【发布时间】:2021-07-23 10:54:06
【问题描述】:

我使用函数来获取根文件夹路径,并启动我的 ASP.NET 应用程序的代码。 但没有工作,它显示错误“长度不能小于零” 以下是我的代码,有人可以给我建议吗?谢谢

        Private Function FindDir(ByVal path As String) As String

        Dim url As String = Request.ServerVariables("SCRIPT_NAME")

        Dim initialSlash As Integer = 0
        Dim CurrentSlash As Integer = 0
        Dim myRootPath As String = Nothing

        initialSlash = url.LastIndexOf("/")
       

        myRootPath = url.Substring(0, initialSlash)

        CurrentSlash = myRootPath.LastIndexOf("/")

        Do While CurrentSlash <> initialSlash And CurrentSlash <> 0
            myRootPath = myRootPath.Substring(0, CurrentSlash)
            CurrentSlash = myRootPath.LastIndexOf("/")
        Loop

        myRootPath = myRootPath.Substring(1)

        Return myRootPath

    End Function

【问题讨论】:

  • 当你可以通过Dim url As String = Request.ApplicationPath得到它时,你为什么要做这一切。
  • 您好@G3nt_m3caj,感谢您的评论和建议。让我了解有关 Request.ApplicationPath 的更多信息。

标签: asp.net vb.net iis


【解决方案1】:

应该是你的Lastindexof()方法没有找到指定的值,所以返回-1。这是错误的原因。

可以参考这个链接解决问题:“Length cannot be less than zero.”

【讨论】:

  • 嗨 Theobald Du,你是对的,我发现 Lastindexof() 方法得到一个“\”。例如。 www.stackoverflow.com\问题。所以返回 0
猜你喜欢
  • 2014-05-26
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2016-05-03
相关资源
最近更新 更多