Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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.

C#
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);


C#
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.
Posted
Updated 28-Jan-16 21:50pm
v3
Comments
Lakamraju Raghuram 29-Jan-16 3:27am    
If I have understood correctly, you want to know how to Navigate using Process.Start(...) if the URL starts with wwww
Sivaji1565 29-Jan-16 4:01am    
Yes.

You can suggest me some other approaches also for navigating.

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.
 
Share this answer
 
Comments
Sivaji1565 29-Jan-16 2:50am    
Thanks for your reply. I will use second approach.
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
 
Share this answer
 
v5
Comments
Sivaji1565 29-Jan-16 3:14am    
Hi Sergey Alexandrovich

Thanks for your irrelevant reply. I really appreciate you for answering my question in your busy schedule, though you have not understood my question as well as behavior of WPF hyperlink. I admit that, everybody can not understand the problem by reading my question title since it is specific to WPF. I request you to read the content of the question too before answering. :)
Sergey Alexandrovich Kryukov 29-Jan-16 10:34am    
Thank you.
Let's see. Isn't your problem having URL "www.*"? Didn't I tell you that you should prepend it with "http://" (scheme)? Did you try it? The idea is: there is no such URL www.mydomain.com. You can type it in the browser's address line, but only because the browser adds "http://" before it. You have to do the same.

Will it answer your question? If not, please explain.

Now, as I an see, you accepted Solution 1 and decided to use "the second approach" (only this is not "append", this is "insert before"). If so, who is not reading posts? I just tried you to explain the nature of the problem. The worst thing is not having no solution. The worst case would be having a solution and not understanding it.

—SA
vrimbaj criaze 30-Jan-16 8:01am    
The accepted solution is 5 lines in length and it is clearly explained. Your explanation ( or advice or confusion) is more than 15 lines and in that many people don't know whether solution is there or not.
Please don't advice people to google it or to refer wiki when questions were posted.
This site is to share knowledge not to advice.
If you are not interested / no idea about the question , let somebody do that work and you mind your own work.
Sergey Alexandrovich Kryukov 30-Jan-16 20:33pm    
No, thank you for you advice. It makes no sense.
—SA
JhonAbraham 1-Feb-16 2:13am    
After reading entire post, I got the following doubts,

- Do you really work in WPF?
- Did you understand the problem ?
- Do you really want to help other developers or do you want to show your arrogance ?
- You have replied as "Total nonsense", do you expect every member in code project should be genius ?
- If everybody knows everything, what for this site is running and why are we here ?

I really wasted 5 minutes of my time for this comment. Please don't waste your time again here. Please help other developers where you can really give solution without using discouraging words.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900