如何在wpf中导航到相对URL(以www而不是http开头) [英] How to navigate to relative URL (starts with www instead of http) in wpf

查看:68
本文介绍了如何在wpf中导航到相对URL(以www而不是http开头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF窗口包含文本框和文本块。用户可以在文本框中输入URL。

按Enter键,文本框的内容将添加到带有超链接的文本块中。以下是我将代码段添加到textblock中的代码段。



 TextBlock textBlk =  new  TextBlock(); 
Hyperlink hLink = new Hyperlink(){NavigateUri = new Uri(URLFromUser,UriKind.RelativeOrAbsolute )};

hLink.RequestNavigate + = hLink_RequestNavigate;

hLink.Inlines.Add(URLFromUser);
textBlk.Inlines.Clear();
textBlk.Inlines.Add( Link:);
textBlk.Inlines.Add(hLink);





  void  hLink_RequestNavigate( object  sender,System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start( new ProcessStartInfo(e.Uri.AbsoluteUri));

e.Handled = true ;

}





案例1:用户输入的网址以http开头



如果单击超链接,则URL将在浏览器中打开。没问题。



案例2:用户输入的网址以www开头



如果超链接单击,它在hLink_RequestNavigate事件中的Process.Start行中崩溃。



如何解决这个问题?



我使用的是.Net 4.5和WPF。



注意:我已经用Google搜索了,但找不到解决方案。





提前致谢。

解决方案

您可以删除 UriKind.RelativeOrAbsolute 参数。这将导致您可以处理的 UriFormatException 。因此迫使用户输入完整的URL。



另一种方法可能是附加 http:// 如果是的话在创建 Uri 对象之前,字符串中不存在。



您还可以使用 WebClient 对象并尝试从提到的URL获取响应,以确保它实际上是有效的现有URL。


抱歉,www而不是http总是胡说八道。它们是不同的东西,不可替换:http或https是方案,www是主机的一部分,www部分本身通常是表示最低级子域,传统上扮演特殊角色,特别是具有www的域。通常映射到相同的名称没有www。。请参阅:

统一资源标识符 - 维基百科,免费的百科全书 [ ^ ],

万维网 - 维基百科,免费的百科全书 [ ^ ],

域名 - 维基百科,免费的百科全书 [ ^ ],

子域名 - 维基百科,免费的百科全书 [ ^ ]。



此事与概念无关相对网址。对于URI的文件方案,URI的路径部分的语法(统一资源定位器 - 维基百科,免费百科全书 [ ^ ])严格遵循文件路径的传统类UNIX语法。相对路径以'。'(当前子目录),'..'(父子目录)或当前目录的直接子项的简单名称开头;此部分可选地后跟目录分隔符'/'(即使在Microsoft系统上使用反斜杠也是令人困惑的)和其他子目录结构。例如:child-of-current-directory / grandchild,../ peer / child,。/ child-of-current-directory /grandchild。



请注意,URI可以是协议相对统一资源定位器 - 维基百科,免费的百科全书 [ ^ ] 。



所以,实际上,如何导航到www。*?哪有这回事。最有可能的是,它是http://www.*或https://www.*。 (除非你使用这种命名方式混淆地创建了一个本地文件系统目录,但答案很明显。)这将是绝对的URI。



-SA

My WPF window contains a textbox and a textblock. User can enter a URL in the textbox.
On pressing Enter button, content of the textbox is added to the textblock with hyperlink. Below is my code snippet for adding the URL into textblock.

TextBlock textBlk = new TextBlock();
Hyperlink hLink = new Hyperlink() { NavigateUri = new Uri(URLFromUser, UriKind.RelativeOrAbsolute) };
                                
hLink.RequestNavigate += hLink_RequestNavigate;
                                
hLink.Inlines.Add(URLFromUser);                                
textBlk.Inlines.Clear();
textBlk.Inlines.Add("Link: ");
textBlk.Inlines.Add(hLink);



void hLink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
        Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
    
        e.Handled = true;

}



Case 1: User entered a URL starting with "http"

If the hyperlink is clicked, the URL is opening in browser. No issues.

Case 2: User entered a URL starting with "www"

If the hyperlink is clicked, it is crashing in "Process.Start" line in "hLink_RequestNavigate" event.

How to fix this issue ?

I am using .Net 4.5 and WPF.

Note: I have googled it already but could not find the solution.


Thanks in advance.

解决方案

You can remove the UriKind.RelativeOrAbsolute parameter. This will cause UriFormatException which you can handle. Thus forcing users to enter complete URL.

Another approach could be to append http:// if it does not exist in the string before creating Uri object.

You can also make use of WebClient object and try to get response from URL mentioned to ensure that it is in fact a valid and existing URL.


Sorry, "www instead of http" is total nonsense. They are different things, not replaceable: "http" or "https" is the scheme and "www" is the part of the host, the part "www" itself normally represents the lowest-level subdomain, which traditionally plays the special role, in particular, the domain with "www." is typically mapped to the same name without "www.". Please see:
Uniform Resource Identifier — Wikipedia, the free encyclopedia[^],
World Wide Web — Wikipedia, the free encyclopedia[^],
Domain name — Wikipedia, the free encyclopedia[^],
Subdomain — Wikipedia, the free encyclopedia[^].

This matter has nothing to do with the notion of "relative URL". For the file scheme of URI, the syntax of the path part of the URI (Uniform Resource Locator — Wikipedia, the free encyclopedia[^]) closely follows traditional "UNIX-like" syntax of file paths. Relative paths starts with '.' (current sub-directory), '..' (parent sub-directory) or the simple name of the immediate child of a current directory; this part is optionally followed by the directory separator '/' (even on Microsoft systems confusingly using backslash) and the rest of the sub-directory structure. For example: "child-of-current-directory/grandchild", "../peer/child", "./child-of-current-directory/grandchild".

Note that the URI can be protocol-relative: Uniform Resource Locator — Wikipedia, the free encyclopedia[^].

So, practically, how to navigate to "www.*"? There is no such thing. Most likely, it's either "http://www.*" or "https://www.*". (Unless you confusingly created a local file system directory using this naming style, but then the answer is quite obvious.) This will be the absolute URI.

—SA


这篇关于如何在wpf中导航到相对URL(以www而不是http开头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆