首页 \ 问答 \ VB.net / asp.net:获取没有文件名的URL(VB.net / asp.net: Get URL without filename)

VB.net / asp.net:获取没有文件名的URL(VB.net / asp.net: Get URL without filename)

我想在VB.net中动态设置一个链接到一个文件。 我的网址如下所示:

http://server/folder/folder2/file.aspx?get=param

我试图使用Request.URL,但我还没有找到任何解决方案只获取

http://server/folder/folder2/

没有查询字符串,也没有文件名。

请帮忙。


i want to set a link in VB.net dynamically to a file. My url looks like that:

http://server/folder/folder2/file.aspx?get=param

I tried to use Request.URL but i have not found any solution to get only

http://server/folder/folder2/

without the query string and without the filename.

Please help.

更新时间:2022-04-23 22:04

最满意答案

您可以使用Request实例轻松获取相关文件路径,然后使用它,使用Path类应该可以提供帮助:

Dim relativePath = Request.AppRelativeCurrentExecutionFilePath
Dim relativeDirectoryPath = System.IO.Path.GetDirectoryName(relativePath)

值得注意的是, GetDirectoryName可能会改变你的斜杠,所以你可以扩展路径:

Dim mappedPath = HttpContext.Current.Server.MapPath(newpath)

所以,为了消除冗余,我们可以缩短这个:

Dim path = _ 
    Server.MapPath( _ 
        Path.GetDirectoryName( _ 
            Request.AppRelativeCurrentExecutionFilePath)))

但是你需要检查可能的例外情况。


You can easily get a relative file path using the Request instance, then work with that, using Path class ought to help:

Dim relativePath = Request.AppRelativeCurrentExecutionFilePath
Dim relativeDirectoryPath = System.IO.Path.GetDirectoryName(relativePath)

It's worth noting that GetDirectoryName might transform your slashes, so you could expand the path:

Dim mappedPath = HttpContext.Current.Server.MapPath(newpath)

So, to remove redundancy, we could shorten this:

Dim path = _ 
    Server.MapPath( _ 
        Path.GetDirectoryName( _ 
            Request.AppRelativeCurrentExecutionFilePath)))

But you'll need to check for possible exceptions.

相关问答

更多
  • 直接去添加引用~然后去调用就可以了
  • vb.net那里下载[2022-12-02]

    官方下载地址:http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express 此为:vb.net express版本,没有时间限制
  • 尝试使用Uri类加载路径并从Segments集合中提取文件名: Uri uri = new Uri("http://www.domain.com/image.jpg"); string fileName = uri.Segments.Last(); Try using the Uri Class to load the path and pull the file name from the Segments collection: Uri uri = new Uri("http://www.domain ...
  • 您可以使用Request实例轻松获取相关文件路径,然后使用它,使用Path类应该可以提供帮助: Dim relativePath = Request.AppRelativeCurrentExecutionFilePath Dim relativeDirectoryPath = System.IO.Path.GetDirectoryName(relativePath) 值得注意的是, GetDirectoryName可能会改变你的斜杠,所以你可以扩展路径: Dim mappedPath = HttpCont ...
  • 如果不联系服务器就无法完成。 只有服务器知道确切的映射。 来自Fiddler:请求 GET http://www.techspot.com/downloads.php?action=download_now&id=2991&evp=113a02f49ca8ac11b566336b984b1655&file=1 HTTP/1.1 ... 响应 HTTP/1.1 301 Moved Permanently Server: nginx Date: Fri, 27 Sep 2013 17:26:14 GMT Co ...
  • 您是否尝试在搜索中添加“VB”一词? http://www.myvbprof.com/2007_Version/MVC_Intro_Tutorial.aspx http://www.asp.net/learn/mvc/tutorial-07-vb.aspx http://geekswithblogs.net/jwhitehorn/archive/2007/12/10/117569.aspx Have you tried adding the word "VB" to your searches?? http ...
  • 我解决了这个问题