如何在C#中获取URL路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19739690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:47:17  来源:igfitidea点击:

How to get URL path in C#

c#asp.net.neturl

提问by Nomi Ali

I want to get the all the path of URL except the current page of url, eg: my URL is http://www.MyIpAddress.com/red/green/default.aspxI want to get "http://www.MyIpAddress.com/red/green/" only. How can I get.I'm doing like

我想获得URL的所有路径,除了URL的当前页面,例如:我的网址http://www.MyIpAddress.com/red/green/default.aspx我想“ HTTP:// WWW。仅限 MyIpAddress.com/red/green/”。我怎样才能得到。我正在做

string sPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString; System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            sPath = sPath.Replace("http://", "");
            System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
            string sRet = oInfo.Name;
            Response.Write(sPath.Replace(sRet, ""));

Its showing exception on new System.IO.FileInfo(sPath) as sPath contain "localhost/red/green/default.aspx" saying "The given path's format is not supported."

它在 new System.IO.FileInfo(sPath) 上显示异常,因为 sPath 包含“localhost/red/green/default.aspx”,说“不支持给定路径的格式”。

采纳答案by jwa

Don't treat it as a URI problem, treat it a string problem. Then it's nice and easy.

不要把它当作一个 URI 问题,把它当作一个字符串问题。然后它又好又容易。

String originalPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString;
String parentDirectory = originalPath.Substring(0, originalPath.LastIndexOf("/"));

Really is that easy!

真的是那么容易!

Edited to add missing parenthesis.

编辑以添加缺少的括号。

回答by Sudhakar Tillapudi

Replace this :

替换这个:

            string sRet = oInfo.Name;
            Response.Write(sPath.Replace(sRet, ""));

With following:

与以下:

        string sRet = oInfo.Name;           
        int lastindex = sRet.LastIndexOf("/");
        sRet=sRet.Substring(0,lastindex)
        Response.Write(sPath.Replace(sRet, ""));

回答by Ratna

use this

用这个

string sPath = (HttpContext.Current.Request.Url).ToString();
sPath = sPath.Replace("http://", "");
var oInfo = new  System.IO.FileInfo(HttpContext.Current.Request.RawUrl);
string sRet = oInfo.Name;
Response.Write(sPath.Replace(sRet, ""));

回答by B H

This may get you want you want if you're just trying to navigate to another page on your site, but it doesn't get the absolute path if you really need that. You can navigate within the site without using the absolute path.

如果您只是想导航到您网站上的另一个页面,这可能会让您想要,但如果您真的需要,它不会获得绝对路径。您可以在不使用绝对路径的情况下在站点内导航。

string loc = "";
loc = HttpContext.Current.Request.ApplicationPath + "/NewDestinationPage.aspx";
Response.Redirect(loc, true);

If you really need the absolute path, you can pick off the parts and build what you need with the Uri class:

如果您真的需要绝对路径,您可以选择部分并使用 Uri 类构建您需要的内容:

Uri myUri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri)
myUri.Scheme
myUri.Host  // or DnsSafeHost
myUri.Port
myUri.GetLeftPart(UriPartial.Authority)  // etc.

Good articleon the subject of ASP.NET paths.

关于 ASP.NET 路径主题的好文章

回答by Shibu Thomas

Main URL : http://localhost:8080/mysite/page.aspx?p1=1&p2=2

主网址:http://localhost:8080/mysite/page.aspx?p1=1&p2=2

Get different parts of URL in C#.

在 C# 中获取 URL 的不同部分。

Value of HttpContext.Current.Request.Url.Host
localhost

Value of HttpContext.Current.Request.Url.Authority
localhost:8080

Value of HttpContext.Current.Request.Url.AbsolutePath
/mysite/page.aspx

Value of HttpContext.Current.Request.ApplicationPath
/mysite

Value of HttpContext.Current.Request.Url.AbsoluteUri
http://localhost:8080/mysite/page.aspx?p1=1&p2=2

Value of HttpContext.Current.Request.RawUrl
/mysite/page.aspx?p1=1&p2=2

Value of HttpContext.Current.Request.Url.PathAndQuery
/mysite/page.aspx?p1=1&p2=2