Obtaining the current URL inline in ASP.NET
I am trying to implement the Facebook Like button on my site, but rather than having to add it to each individual page, I would rather add it once to the master page 开发者_StackOverflow社区and use the current URL. I have tried the following code to obtain the current URL:
<a href="<% Request.Url.AbsoluteUri.ToString(); %>">Link</a>
However, when it runs, say on http://localhost:1234/About.aspx, the link only points to http://localhost:1234 rather than the full address.
What am I doing wrong, or is it not possible to achieve it this way?
When you want to write a text inline with the HTML from a server code tag, you need to do a Response.Write since a server code tag <% %>
for asp.net only reads a code inside of it. To write it in the HTML wither put it in Response.Write
method or <%= %>
tag.
<a href="<% Response.Write(Request.Url.AbsoluteUri.ToString()); %>">Link</a>
or
<a href="<%=Request.Url.AbsoluteUri.ToString() %>">Link</a>
Try this:
<a href="<%= Request.Url.AbsoluteUri.ToString(); %>">Link</a>
<a href="<%= Request.Url.AbsoluteUri %>">Link</a>
should work.
精彩评论