获取页面的当前根路径 url
Get current root path url of the page
我有函数用于获取根文件夹路径和我的 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
应该是你的Lastindexof()方法没有找到指定的值,所以返回了-1。这是错误的原因。
可以参考这个link解决问题:“Length cannot be less than zero.”
我有函数用于获取根文件夹路径和我的 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
应该是你的Lastindexof()方法没有找到指定的值,所以返回了-1。这是错误的原因。
可以参考这个link解决问题:“Length cannot be less than zero.”