获取没有查询字符串的网址

时间:2011-01-07 21:01:02

标签: c# asp.net

我有这样的网址:

http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye

我想从中获取http://www.example.com/mypage.aspx

你能告诉我怎样才能得到它?

18 个答案:

答案 0 :(得分:355)

这是一个更简单的解决方案:

var uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = uri.GetLeftPart(UriPartial.Path);

从这里借来:Truncating Query String & Returning Clean URL C# ASP.net

答案 1 :(得分:124)

您可以使用System.Uri

Uri url = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = String.Format("{0}{1}{2}{3}", url.Scheme, 
    Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);

或者您可以使用substring

string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path = url.Substring(0, url.IndexOf("?"));

编辑:修改第一个解决方案以反映brillyfresh在评论中的建议。

答案 2 :(得分:37)

这是我的解决方案:

Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);

答案 3 :(得分:33)

Request.RawUrl.Split(new[] {'?'})[0];

答案 4 :(得分:28)

这里也找到了很好的答案source of answer

Request.Url.GetLeftPart(UriPartial.Path)

答案 5 :(得分:13)

我的方式:

new UriBuilder(url) { Query = string.Empty }.ToString()

new UriBuilder(url) { Query = string.Empty }.Uri

答案 6 :(得分:10)

您可以使用Request.Url.AbsolutePath获取页面名称,使用Request.Url.Authority获取主机名和端口。我不相信有一个内置属性可以准确地给你你想要的东西,但是你可以自己组合它们。

答案 7 :(得分:3)

这是使用@ Kolman的答案的扩展方法。使用Path()比使用GetLeftPart更容易记住。您可能希望将路径重命名为GetPath,至少在将扩展属性添加到C#之前。

用法:

Uri uri = new Uri("http://www.somewhere.com?param1=foo&param2=bar");
string path = uri.Path();

班级:

using System;

namespace YourProject.Extensions
{
    public static class UriExtensions
    {
        public static string Path(this Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            return uri.GetLeftPart(UriPartial.Path);
        }
    }
}

答案 8 :(得分:3)

拆分()变化

我只想添加此变体以供参考。网址通常是字符串,因此使用Split()方法比Uri.GetLeftPart()更简单。并且Split()也可以使用relative,empty和null值,而Uri会抛出异常。此外,Url还可能包含散列,例如/report.pdf#page=10(在特定页面打开pdf)。

以下方法处理所有这些类型的网址:

   var path = (url ?? "").Split('?', '#')[0];

示例输出:

答案 9 :(得分:1)

Request.RawUrl.Split( '?')[0]

仅限网址名称!!

答案 10 :(得分:0)

    string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
    string path = url.split('?')[0];

答案 11 :(得分:0)

Silverlight解决方案:

string path = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);

答案 12 :(得分:0)

我创建了一个简单的扩展程序,因为如果没有package main import ( "fmt" "gopkg.in/yaml.v2" "io/ioutil" "log" "os" ) type Document struct { Runs []Runs `yaml:"runs,omitempty"` } type Runs struct { Name string `yaml:"name,omitempty"` Type string `yaml:"type,omitempty"` Path string `yaml:"path,omitempty"` Builds []Builds `yaml:"builds,omitempty"` } type Builds struct { Name string `yaml:"name,omitempty"` Properties map[string]string `yaml:"properties,omitempty"` } func main() { var document Document reader, err := os.Open("demo.yml") if err != nil { log.Fatal(err) } buf, _ := ioutil.ReadAll(reader) yaml.Unmarshal(buf, &document) if err := yaml.Unmarshal(buf, &document); err != nil { fmt.Print(err) os.Exit(1) } fmt.Println(document) } 开头的话,其他几个答案会抛出空例外:

QueryString

用法:

public static string TrimQueryString(this string source)
{ 
    if (string.IsNullOrEmpty(source))
            return source;

    var hasQueryString = source.IndexOf('?') != -1;

    if (!hasQueryString)
        return source;

    var result = source.Substring(0, source.IndexOf('?'));

    return result;
}

答案 13 :(得分:0)

简单示例将使用子字符串,如:

string your_url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path_you_want = your_url .Substring(0, your_url .IndexOf("?"));

答案 14 :(得分:-1)

System.Uri.GetComponents,只是您想要的指定组件。

Uri uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped);

输出:

http://www.example.com/mypage.aspx

答案 15 :(得分:-1)

var canonicallink = Request.Url.Scheme + "://" + Request.Url.Authority + Request.Url.AbsolutePath.ToString();

答案 16 :(得分:-2)

this.Request.RawUrl.Substring(0, this.Request.RawUrl.IndexOf('?'))

答案 17 :(得分:-2)

试试这个:

urlString=Request.RawUrl.ToString.Substring(0, Request.RawUrl.ToString.IndexOf("?"))

来自:http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye 你会得到这个:mypage.aspx