Explicitly treat link as an absolute link? - c#

I have a control on one of my page which takes some user input (URL) and allows the user to test the link.
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("URL") %>' Target="_blank">Click here to test link</asp:HyperLink>
The problem is if the user puts in the URL "google.com", then the link will be treated as "http://localhost/google.com". I have to put "http://www.google.com" for it to go to the right place.
Is it possible to treat the link like it is an absolute link in all cases?

Try this...
NavigateUrl='<%# Eval("URL", "http://{0}") %>'
Edit: if you want to add check whether it already contain http:// then it should be like..
NavigateUrl=<%# Eval("URL").ToString().Contains("http://") == true ? Eval("URL") :
"http://" + Eval("URL") %>

Is this question of academic nature or do you have a problem to solve?
Firstly, the notion that http://www.google.com is the right place and http://localhost/google.com is incorrect is very wrong.
Your problem as I see is this.
When you pull urls from a datasource there are raw urls which ASP.NET Framework recognizes as relative URL. But you would like them to use their absolute uri.
But the problem is not with the framework as http://localhost/google.com could be a valid URL.
A good example for the same will be the site builtWith. It has valid URLS like http://builtwith.com/facebook.com
And so, the problem is at the place where you have input the data. Why could you not place a small combo so that users could easily add the protocols and then append the url to it as shown below?
Hope this helps.

Based on your comments:
What if it's a secure (https) site or an FTP site?
and
Would that work? What if the input already has http in it? What if it's a secure (https) site or an FTP site?
and
The problem is if the user puts in the URL "google.com", then the link will be treated as "http://localhost/google.com". I have to put "http://www.google.com" for it to go to the right place.
Is it possible to treat the link like it is an absolute link in all cases?
You're asking for magic. We don't specialize in magic here. You have to specify something that lets you know that it can become a specific type of domain. Tell them if they don't specify the protocol you will as http:// and the rest of the time it's up to them to specify the protocol. It's all you can do.
As for determining if it's got a protocol, check string.Contains("://"), since that's what the browser is going to do anyway, more or less.

In your code-behind, check if the user input contains "http://" and append it if it doesn't. You can also use JavaScript to do this.

Related

HttpContext.Current.Request.Url.AbsolutePath with Master Page

I am using HttpContext.Current.Request.Url.AbsolutePath as part of my security which the user needs to login before they access certain pages.
In this example and it works great. The user has to sign in before they view their profile.
if (Session["UserID"] == null)
{
Response.Redirect("Login.aspx/?ReturnURL=" + HttpContext.Current.Request.Url.AbsolutePath);
}
The end result looks like this:
http://localhost:54324/Login.aspx/?ReturnURL=/Profile_Page.aspx
The issue I am having is the pages are part of the master page. When the redirect occurs to the login page, nothing on the master page works. The navigation links do not fire, the images show broken links, etc. However when I access the Login Page directly everything in the master page works fine.
There's an extra forward slash in your redirect URL:
Response.Redirect("Login.aspx/?ReturnURL=" + HttpContext.Current.Request.Url.AbsolutePath);
^
Your login page still loads correctly because ASP.NET treats the slash as a separator for additional path information, similar to the way that a question mark is the separator for the query string.
But the extra slash causes a browser to resolve relative URLs for links and images relative to a child directory named Login.aspx instead of relative to the root of your application. For example, if you had the image <img src="Logo.png">, a browser would attempt to load Login.aspx/Logo.png. Removing the forward slash should fix the problem with the redirect:
Response.Redirect("Login.aspx?ReturnURL=" + Request.Url.AbsolutePath);
Nevertheless, visitors will still get broken URLs if they manually append the slash. To avoid this, use the built-in <asp:HyperLink> and <asp:Image> server controls, which will generate relative URLs taking the extra slash into account:
<asp:HyperLink runat="server" NavigateUrl="~/About.aspx" Text="About Us" />
<asp:Image runat="server" ImageUrl="Logo.png" />

Having issue with using bookmark links with url routing

I have url routing setup on this site. I have a bookmark link in a nav that goes to a item in the home page like so:About . The problem is that, when someone entered the url incorrectly like site.com/contact/, instead of making the url site.com/default.aspx#about, it is making the url site.com/contact/default.aspx#about. All the other urls in the nav use the correct path. I know it has to do with the hashtag. Is there a way around it?
Instead of having
About
Try this:
<a href='<%= ResolveUrl("~/default.aspx#about") %>'>About</a>
This should always write the correct URL regardless.

URL is not rendering correctly in C#

I have Hyperlink in a datalist. HTML is given below:
<asp:HyperLink ID="lnkEntry" runat="server"><%# DataBinder.Eval(Container.DataItem, "Title") %></asp:HyperLink>
i have saved the URL of a Entry in the Database, like "http://test.com/posts/New-test-in-March" And used the following way to Bind it to Hyperlink in Item_databound of Datalist.
lnkEntry.NavigateUrl = objEntry.PermaLink;
But when it renders into the browser the URL get changed to "http%3a//test.com/posts/New-test-in-March"
i have tried to use the URLDecode but it doesn't make any change in output.
lnkEntry.NavigateUrl = Server.UrlDecode(objEntry.PermaLink);
Please help me how can i fix this issue.
you should use HttpUtility, like this:
lnkEntry.NavigateUrl = HttpUtility.UrlEncode(objEntry.PermaLink);
or WebUtility if running recent versions of .net.
Check this thread for more info.

I want to hide URL in the addressbar

I want to display the report(pdf) which was available in the webpath assume (http://myservre/reports/myrepoft.pdf). Currently, It was being displayed via IFrame like this.
strReportPDFFile = "`http://myservre/reports/myrepoft.pdf`"
<IFRAME frameborder="1" onload="onchangestate()" src="" id="ifrmShowReport" frameborder="0" height="100%" width="100%" marginwidth="0" align="top" style="overflow: none;overflow-x: auto;display:none"></IFRAME>
document.getElementById("formContainer").style.display = "block";
document.getElementById("ifrmShowReport").style.display = "block";
document.getElementById("ifrmShowReport").src = strReportPDFFile;
The URL was displaying in the address bar which I want to hide URL. Is it possible ? If not, please suggest some better alternative.
I argue that the main reason of your question (hiding the URL of the PDF file) is for security issues.
E.g. you have some PDF files on your server that have to be sent and read only from intended audience and not from everybody.
I give you some basic hints on how to realize this intent:
place the PDF files out of the public root of your webserver
realize a server side script that:
check is the user is authenticated and have the grant to get the file
get some parmeters to retrieve the correct file
serve the file with this server side script content-disposition (as attachment, sending the correct Mime-Type and a "ficticious" URL)
Since you put C# in your question tags i address you on
Add a content-disposition to the header with C#:
Response.AddHeader("content-disposition", #"attachment;filename=""MyFile.pdf""");
Then you can display your pdf wherever u wish: blank page, iframe ...
It is sure you'll not find a solution just working with HTML/Javascript, not one of those easly to avoid for a medium level internet user.

MSCRM Sitemap link to .htm page. How do I pass parameters?

I have a Site Map link that opens up a Web Resource .htm page. Everything works great except I want to pass some values to that page. I assumed I could pass them as a querystring since there is the Xrm.Page.context.getQueryStringParameters() method but my querystring seems to get stripped out.
<SubArea Icon="$webresource:my_webresourceicon.gif"Id="nav_my_webresourceid" Url="$webresource:my_webresourcepage.htm?xyz=123" Client="All" AvailableOffline="false" Title="My Web Resource Page" Description="My Web resource Description" Sku="All" PassParams="true"/>
I want to be able to access the parameter xyz from my_webresourcepage.htm as the page loads. What is the best way to go about passing this value?
Thanks
Unfortunately you can't pass xyz unless you modify the registry. Instead, you want to use the parameter "data".
http://msdn.microsoft.com/en-us/library/gg309536.aspx

Categories

Resources