Add HttpHandler from HttpModule for static file not present in project - asp.net

How to set httphandler from httpmodule for static file that is not present in poject.
Eg: If client requests a file called abc.xml thats not in my project,I need to add handler from my httpmodule and send some response to client how do i do it. My handler does not get called after adding it from httpmodule.
My HttpModule Code is as given below:
Public Sub Context_BeginRequest(sender As Object, e As EventArgs)
Dim Application As HttpApplication = CType(sender, HttpApplication)
Dim CustomHandler As StorageHandler = New StorageHandler()
Application.Context.Handler = CustomHandler
End Sub
The processrequest in customHandler does not get executed.

After spending good 5hr, Got it .
Need to use RemapHandler,
Application.Context.RemapHandler(CustomHandler)
and not
Application.Context.Handler = CustomHandler

Related

asp.net VB.net file download handler not work properly

I have big file (about 2GB) to distribute to our customer, My website is written by asp.net vb, this is my file download handler:
Public Class FileHandler
Implements IHttpHandler
Public Sub ProcessRequest(ByVal httpcontext As HttpContext) Implements IHttpHandler.ProcessRequest
If HttpContext.User.Identity.IsAuthenticated Then
Dim FileName As String = HttpContext.Request.QueryString("File")
HttpContext.Response.Buffer = False
HttpContext.Response.Clear()
HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" & FileName)
HttpContext.Response.ContentType = "application/exe"
HttpContext.Response.TransmitFile("~/download/ExE/" & FileName)
HttpContext.Response.Flush()
HttpContext.Response.Close()
End If
End Sub
Public ReadOnly Property IsReusable As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
My problem is this handler sometimes could not work properly.
Most customer could download it by this handler, but some customer click the download link, it will endless waiting for server's response, after long time waiting, it shows the error page says the IE cannot display the webpage. some customer try to download the file from IE8, it will show the error page directly.
I am really appreciate any one can help with this issue. Thank you!
I use a button or just a plain link to the file itself, I've never had to use a handler to download files.
For example, on the aspx I have a button and in the code behind I send the user to the file using the response object:
Protected Sub Button_DownloadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_DownloadFile.Click
Response.AddHeader("Content-Disposition", "attachment; filename=TheFileName.ext")
Response.WriteFile("~/App_Data/TheFileName.ext")
END SUB

Update UI ASP.NET Control - Asynchronous (CallBack Approach) - Calling WebService Methods

Here is the sample code for WebService written in ASP.NET 1.1 using VB.NET:
<System.Web.Services.WebService(Namespace := "http://tempuri.org/WebService_Simple/Service1")> _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function Add(ByVal A As Integer, ByVal B As Integer) As Integer
System.Threading.Thread.Sleep(2000)
Return A + B
End Function
End Class
Here is the Client code:
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim proxy As New Service1
Dim ac As New AsyncCallback(AddressOf MyCallback)
Dim ar As IAsyncResult = proxy.BeginAdd(10, 20, ac, proxy)
System.Threading.Thread.Sleep(1000)
End Sub
Public Sub MyCallback(ByVal ar As IAsyncResult)
Dim proxy As Service1 = CType(ar.AsyncState, Service1)
Dim result As Integer = proxy.EndAdd(ar)
Label1.Text = "Result = " & result.ToString()
End Sub
End Class
I am using Visual Studio.NET 2003 (ASP.NET 1.1 / VB.NET). I am getting the right values into "result" variable. But it is NOT updating the UI element (Label1). I know the reason. Because UI - MAIN - thread is different than the one which is using CALLBACK - WORKER - thread. Can you provide me sample code so that it will update the UI (Label1) control using VB.NET in Visual Studio.NET 2003 ?
Thanks
This is the answer. It is negative but it is answer. You can't and shouldn't do this in ASP.NET Web Forms because Page cycle moves through well known set of steps. There shouldn't be any async/multi-threaded programming done in page cycle methods. and you definitely shouldn't make ASP.net thread sleeping.
Your Button1_Click is part of the cycle and when it executes, ASP.net framework already executes something else. And whatever you call asynchronously is just left behind. The code executes but values never affect anything.
If you trying to achieve asynchronous behavior in ASP.net web forms, you need to include some client-side devices that will call your web service. I am not even sure now that the old framework you use has something like update panel or there is AJAX toolkit for it. Bu that is how you need to achieve it without page reloading - you use jQuery or something else to call your web service. What you tried to do in your example is a dead end.
And seriously, start using new frameworks. At least 3.5.

Lost HttpContext in a custom event

I'm getting the 'Response is not available in this context error' calling the following function:
Private Sub ReloadPage(ByVal inNumber As Integer) Handles tempaux.Advertise
'Response.Redirect("tope.aspx?dep=" & CStr(inNumber))
Response.Write("<script>window.open('tope.aspx?dep= & CStr(inNumber)','topFrame');</script>")
End Sub
I've changed the line adding the System.Web.HttpContext.Current before Response.Write and I get 'Object reference not set to an instance of an object'.
To give some background: tope.aspx is, as you can see, opened in topframe. As soon as it loads it starts a CustomTimer object I've defined:
Public Class tope
Inherits System.Web.UI.Page
Public funciones As funciones = New funciones
Dim WithEvents tempaux As CustomTimer = Global.objCustomTimer
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim inUserProfile As Int64
Try
tempaux.StartTimer()
Catch ex As Exception
'bla bla
End Try
As you can see I've declared the CustomTimer in the Global.asax. The CustomTimer object raises an Advertise event every 5 seconds and passes 'inNumber' as a parameter for the tope.aspx page to refresh some label, a simple thing. CustomTimer is a class I made to manage the timer, it doesn't inherits any other class( For what I've learned in my search it has to inherit some httpthing but I'm not sure). I'm guessing that at some point the httpcontext is being lost (I've searched in google and I couldn't figure its lifecycle or whatever information that tells me why it 'dies). Can anyone help me to find out what is the problem?
thanks
Your timer exists outside of the tope page class, so it is possible that the timer event is firing after the response from the page is complete and there is no longer a HttpContext.Current instance.
It sounds like what you are trying to do is to change an advertising banner on a page every 5 seconds, once the page is loaded. You need to do that using a javascript timer, which would fire every 5 seconds and make a request back to your web server for a new advertisement.

Why am I getting an weird error on my asp.net webpage?

I am currently developing a asp.net webpage and a WCF publish subscribe service. The WCF service has been done up the subscriber is a winform app which is also done up. I am now trying to get the asp.net webpage to connect to my publish service for my WCF. However there is an weird error that I am getting. I have added the app.config and the generatedProxy.cs.vb to my asp.net project.
This is the code for my class
Public Class json
Implements IPostingContractCallback
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.ContentType = "application/json"
If Request.QueryString("action") = "postAlert" Then
Dim site As InstanceContext = New InstanceContext(New json())
Dim client As PostingContractClient = New PostingContractClient(site)
client.PublishPost("testing")
client.Close()
End If
End Sub
Public Sub PostReceived(ByVal postSampleData As String)
Console.WriteLine("PostChange(item {0})", postSampleData)
End Sub
the sub PostReceived is the callback method for my WCF service. It practically is meant to be do nothing as this is the Publisher, but I still have to implement it due to the WCF standards. The error that I am getting is
Class 'json' must implement 'Sub PostReceived(postSampleData As String)' for interface 'IPostingContractCallback'
How come i am getting the error when i have already implemented the sub as stated above?
Method signature must be: (Take a look at VB.NET Interfaces)
Public Sub PostReceived(ByVal postSampleData As String) Implements IPostingContractCallback.PostReceived
Console.WriteLine("PostChange(item {0})", postSampleData)
End Sub

to rewrite url in Global.asax, ASP.NET

HEllo. I need replace double slashes in one slash. I am planning do this in Global.asax Application_BeginRequest event. Is it enough? or better do a http module?
Thank you.
UPD
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo(AppSettings.UsedCulture)
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture
Dim retUrl As String
....
some code
....
'Dim app As HttpApplication = CType(sender, HttpApplication)
'app.Context.RewritePath(retUrl)
Dim myContext As HttpContext = HttpContext.Current
'Rewrite the internal path
myContext.RewritePath(retUrl)
End If
I am using .Net 1.1. And It must be on .Net 1.1
RewritePath does not rewrite URL.Why?
UPD2
Having decided to make addition redirect in Sub Application_BeginRequest with new rewritting url.
If you're using IIS 7.0 (and newer), you can use the IIS UrlRewrite module. We've been using it for a while, and have no complaints.

Resources