Remove file extension on download using an ASP.NET HttpHandler? - asp.net

I have stored file in my server, clients can download files via my page Home.aspx by clicking on an anchor, but i want to edit the original filename before launching download. How can i do it using an ASP.NET HttpHandler. Thanks in advance.
This is my anchor code:
<% if (document.Path != null) { %>
<a id="downloadLink" runat="server" style="margin: 5px" href="<%# CONTENT_DIRECTORY_ROOT + document.Path %>">
<%= LanguageManager.GetValue("LABEL_DOWNLOAD") %></a>
<% } %>

Try the RewritePath(newUrl) method
HttpContext.Current.RewritePath(sUrl)
Or you could use
HttpContext.Current.Server.Transfer(sUrl)

Related

Rails 7.0 with turbo back button is not working

I am building an app with rails 7.0
<turbo-frame id="<%= item.id %>">
<div><%= item.id %></div>
<div><%= item.item_type %></div>
<a href="<%= edit_item_path(item.id) %>"
data-turbo-frame="_self">
edit item
</a>
</turbo-frame>
The edit view loads the new content successfully, but there is no back button functionality on this scenario. How can I enable back button?
I had to add data-turbo-action="advance" in my link, although according to the documentation that behaviour should be the default one.
<a href="<%= edit_item_path(item.id) %>"
data-turbo-frame="_self"
data-turbo-action="advance">

<%# Eval("") %> in href prefixes relative path in aspx page

I have an .aspx in which there is one anchor tag whose href property is set by the server side code i.e. DataTable.
My site url is : [xxx/Pages/Home.aspx] and suppose the href from the DataTable is bound http://www.google.com then the link redirects to [xxx/Pages/http//www.google.com] instead of http://www.google.com . Somehow it prefixes relative url of page.
My ascx file is :
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<asp:ListView ID="Feed" runat="server">
<ItemTemplate>
<div class="Main">
<div class="Ttile">
<a href="<%# Eval("Link") %>" target="_blank" title="<%# Eval("Title") %>" ><%# Eval("Title") %></a>
</div>
</td>
</tr>
</table>
I want to redirect the user to http://www.google.com when user clicks on the link but the anchor tag redirects to http://xxx/Pages/http//www.google.com
If I put <%# Eval("Link") %> outside the anchor tag then it displays the proper url like : http://www.google.com. It means the data in "Link" column is perfect
How should I tackle this issue?
try this one
<%# RemoveServerUri(Convert.ToString(DataBinder.Eval(Container.DataItem, "Link")))
C#
public string RemoveServerUri(string link)
{
link = link.Replace("xxx/Pages/", "");
return link;
}

repeater how to avoid blank html

We currently have an application which we have a repeater
<asp:Repeater ID="rptOfficials" runat="server">
<ItemTemplate>
<tr>
<td>
<p class="officials">
<%#Eval("OffPosition") %> <%#Eval("FullName") %>
</p>
<p class="officials">
<%#Eval("Phone") %>
</p>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
The Phone field is not always supplied, when not there a blank paragraph tag is generated, we're thinking of changing
<p class="officials">
<%#Eval("Phone") %>
</p>
to either:
<p class="officials">
<asp:label id="lblPhone" runat="server"/>
</p>
or:
<p class="officials">
<%#GetPhone("OffId") %>
</p>
and then have either the label return the phone number with the paragraph tags wrapped around it, or have the function return it. Is there a better way to get around this?
It depends how your Binding model is composed but I suggest adding the logic in your property (Phone) on your page your (view)model (yes you can and should use ViewModels in web forms as well).
That way you keep logic outside the view.
public string Phone {
get {
return this.phone ?? string.Empty;
}
}
That way your logic stays nicely inside your view model and your view just takes care of binding it.
If you don't want to generate the -tag, you can add a boolean property to your model.
public bool DisplayPhone{
get {
return this.Phone == string.Empty;
}
}
You then need to bind it to the visible property as Halcyon mentioned. (note you need a runat="server" for this)
<p runat="server" class="officials" visible='<% #Eval("DisplayPhone") %>'>
<% #Eval("Phone") %>
</p>
I would not recommend using "display: none" since it will keep the space of the p tag. Instead, make the p tag a server tag and set its visibility to false.
<p runat="server" class="officials" visible='<% !string.IsNullOrEmpty(#Eval("Phone")) %>'>
<% #Eval("Phone") %>
</p>
Instead of using
<p class="officials">
<%#GetPhone("OffId") %>
</p>
I prefer generate <p class="officials">foo</p> inside GetPhone(), then you can return nothing when Phone is empty.
You can add small logic like the following -
<%# string.IsNullOrWhiteSpace(Eval("Phone").ToString()) ?
"" : ("<p class=\"officials\">" + Eval("Phone") + "</p>")%>
However, if you want to add more logic, I would like to suggest to use Repeater.ItemDataBound Event
try with this
<p class="officials" <%#string.IsNullOrEmpty(Eval("Phone").ToString())? "style='display:none;':''" %> >
<%#Eval("Phone") %>
</p>

Append filename to Img src attribute using resource file

When I try to append the image name onto the end of the <%$ Resources:LocalizedText, MyKey %> the server doesn't build the path at all.
This is what I'm doing and it's not working:
<img id="Img2" runat="server" src="<%$ Resources:LocalizedText, ImagesPath %>feature_LED.jpg" />
Is there any way to append?
this will work instead
src="<%= Resources.LocalizedText.ImagePath %>

target=_blank opening with same url

I added HyperLink control in my page. When I enter "http://www.google.com", It is opening in new window (good) and pointing google.
1. When I enter "www.google.com", It is opening in new window, but Url is "http://mysite.com/www.google.com". Why this is happing? How to should point to www.google.com
<a href= '<%# Eval("ConferenceUrl") %>' runat ="server" id="ConferenceUrl"
target="_blank"> <%# Eval("ConferenceUrl")%> </a>
You need http:// in the url, I believe.
The <%# Eval("ConferenceUrl") %> needs to have the http:// prefix. Either change your data or add it to the href attribute.
<%# Eval("ConferenceUrl")%>
you have to add http:// in the beginning of href

Resources