Browser not caching handler response unless query string present - asp.net

I have written an asp.net http handler that returns javascript. When including a script tag on html page that references this handler, chrome/firefox is not reporting the cache-control:max-age header being returned unless a querystring (or '?') is present in the script tag src. Thus the handler is being hit every time. This is happening in both firefox and chrome.
Here is the handler implementation:
Public Overrides Async Function ProcessRequestAsync(context As HttpContext) As Task
Dim loader As New ScriptLoader(context)
Dim sb As New StringBuilder()
sb.Append(Await loader.GetScriptsAsync())
sb.Append(loader.GetLoaderScript("engine"))
sb.Append(loader.GetLoaderScript("styles"))
Dim script As String = sb.ToString()
context.Response.ContentType = "application/javascript"
context.Response.Cache.SetCacheability(HttpCacheability.Public)
context.Response.Cache.SetMaxAge(TimeSpan.FromHours(12))
context.Response.Write(script)
End Function
HTML script tag:
<div>
<script src="/ListingDisplay/loader/js"></script>
</div>
Results:
https://i.ibb.co/hK8TQJ3/Capture.png
When I append a "?" on the script source:
<div>
<script src="/ListingDisplay/loader/js?"></script>
</div>
I get the expected results, and the handler is not hit on subsequent requests:
Expected Result:
https://i.ibb.co/x8S3W3v/Capture.png
Anyone ever seen this? Any ideas?

Related

Access winform variable from browsercontrol

I have an application in ASP.Net Ajax. I want to open it via a browsercontrol from a winform, and I wish to access a variable (username) that the user used to log in to the webform with. On load I would like to read that username and perform the rest of my webpage code on that browsercontrol using that username.
My ASP.Net Ajax has been published to a internal web server and the browsercontrol loads that IP address.
Is there any way to achieve this at all?
EDIT:
I have discovered the javascript extension: window.external
And I can call a C# procedure from the webpage using javascript with it, which is a start, but I need to retrieve a varaible from c# - this is where the problem comes in. I have tried the
var name = function window.external.GetGlobalVariable(MyGlobalProcedure, "Cannot Get Value");
But javascript error says the method cannot be applied to the object.
Your answer should be as follows:
Public Class Form1
Dim GlobalVar As String = "Your Name"
Dim YourBrowser As New WebBrowser
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
YourBrowser.Url = New Uri("Your URL address")
AddHandler YourBrowser.DocumentCompleted, AddressOf PageLoadComplete
End Sub
'The invokescript will only work once the HTML has finished loading itself into your WebBrowser
Sub PageLoadComplete()
'Must declare the string inside an array as the invokescript only allows an object to be sent
Dim VarToSend As String() = {GlobalVar}
YourBrowser.Document.InvokeScript("yourJavascriptfunction", VarToSend)
End Sub
The javascript section should look as follows:
<script type="text/javascript" language="javascript">
function userNameSet(name) {
$(document).ready(function() {
//variable now exists inside your WebBrowser client and can be used accordingly now
alert(name);
});
}
</script>
References for answer: http://www.dotnetcurry.com/showarticle.aspx?ID=194
"Store that name in a session variable and access the session in your ajax call"
In your ASP.Net application create a hidden field (or if it's somewhere on the UI in some control that works also). Put the username or whatever information you want to share into that field.
From your WinForms program you can request that field through the WebBrowser control like this:
MessageBox.Show(WebBrowser1.Document.GetElementById("txtUsername").GetAttribute("value"))
The above assumes you have some HTML element called txtUsername with the value attribute set.

asp.net <form> tag getting stripped out of asp:literal

Okay... I know having nested tags is not officially supported. But stay with me on this one..
I have an ASP.NET web page with the standard <form runat=server> tag at the top. I am pulling in a Form (and associated fields) from a 3rd party source via HttpWebRequest on the server side (code behind). I can verify the data I get contains a <form> tag -- via a Trace statement. Then I assign the data to my literal like this:
Dim objRequest As System.Net.HttpWebRequest = System.Net.WebRequest.Create(url)
Dim objResponse As System.Net.WebResponse
objRequest.Method = "POST"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData.ToString)
objRequest.ContentType = "application/x-www-form-urlencoded"
objRequest.ContentLength = byteArray.Length
Dim dataStream As Stream = objRequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
objResponse = objRequest.GetResponse()
Dim streamRead As New StreamReader(objResponse.GetResponseStream())
Dim strResponse As String = streamRead.ReadToEnd()
me.litCMSForm.Text = strResponse
When the page is rendered, somehow .NET removed the <form> tag that was within the literal.
I also tried assigning the variable "strResponse" to a public variable to be displayed and it too had the tag stripped out. And I tried assigning the variable to an asp:Label as well with no luck. And I tried assigning the value to the literal on the "PreRender" function with no success.
Any thoughts on this and why .NET is stripping out the <form> tag?
Thanks!
You're correct - nested form tags are not supported. What you can do is generate JavaScript that would pass the string to an HTML container outside of ASP.NET form or, better yet, output it to an independent iframe.
I had the same problem. I was able to work around the situation by adding another form before the one I really wanted to insert. It seems like ASP was only frisky enough to strip out the first firm it found.
Perhaps this would work for you:
me.litCMSForm.Text = "<form name='fakeOut'></form>" + strResponse
I actually had my form code be a little fancier (I gave it an action and an hidden input). I didn't continue to test to see what could be taken away because it worked.

How to manitain session values during HttpWebRequest?

In my code I'm sending a HttpWebRequest to a page in my website.
When request sends to this page, It doesn't maintain the Session values.
Below is the code, from where I'm generating the web request:
Public Overloads Shared Function ReadURL(ByVal sUrl As String) As String
Dim sBody As String
Dim oResponse As HttpWebResponse
Dim oRequest As HttpWebRequest
Dim oCookies As New CookieContainer
oRequest = WebRequest.Create("http://localhost:64802/inventory/purchase_order.aspx?id=5654")
oRequest.CookieContainer = oCookies
oResponse = oRequest.GetResponse()
Dim oReader As New StreamReader(oResponse.GetResponseStream())
sBody = oReader.ReadToEnd
oReader.Close()
oResponse.Close()
Return sBody
End Function
Below is the code written on Page_Load of Purchaseorder.aspx.vb:
iDomains_ID = Session("Domains_ID")
iLogin_ID = Session("Login_ID")
sPage = Request.Path
If Request.QueryString.Count > 0 Then sPage &= "?" & Request.QueryString.ToString()
sPage = shared01.Encrypt(sPage, Application("PK"))
If Not User.Identity.IsAuthenticated Or iLogin_ID = 0 Then
Response.Redirect("/login.aspx?page=" & sPage)
Exit Sub
End If
Above code doesn't gets the session values and it redirects to the login page.
So, how i can maintain the session on both pages during HttpWebRequest.
Looking for your replies.
EDIT
I've tried to use CookieContainer class as you can see in above code. But it doesn't work at all.
As an alternative, assuming the calling and called pages are in the same application, you could use the Server.Execute method to load the content of the page without making a separate request to the site:
Public Overloads Function ReadURL(ByVal sUrl As String) As String
Using writer As New StringWriter()
Server.Execute("~/inventory/purchase_order.aspx?id=5654", writer, False)
Return writer.ToString()
End Using
End Function
If I've understood you correctly, you're making a request from one page in your site to another, and you want to send the cookies from the current HttpRequest with your WebRequest?
In that case, you'll need to manually copy the cookies to the CookieContainer:
For Each key As String In Request.Cookies.AllKeys
Dim sourceCookie As HttpCookie = Request.Cookies(key)
Dim destCookie As New Cookie(sourceCookie.Name, sourceCookie.Value, sourceCookie.Path, "localhost")
destCookie.Expires = sourceCookie.Expires
destCookie.HttpOnly = sourceCookie.HttpOnly
destCookie.Secure = sourceCookie.Secure
oCookies.Add(destCookie)
Next
NB: You'll either need to make the ReadUrl function non-Shared, or pass the current HttpRequest as a parameter.
You'll also need to make sure the calling page has EnableSessionState="false" in the <%# Page ... %> directive, otherwise the page you're calling will hang trying to obtain the session lock.
Your code seems like you will need to make a request and a post. The first request will redirect you to your login page. The second will be a request where you post to the login page, which will start the session and (?) store information into the session variables. That post (to the login page) will then redirect you to the page you want.
I used code in this example http://www.codeproject.com/Articles/145122/Fetching-ASP-NET-authenticated-page-with-HTTPWebRe (I tweaked it a bit) to write an application to do this.

How to call a javascript function from asp.net Sub functions?

So after extensive search, I found that in order to trigger a javasript function, we could do this:
<script>
function foobar()
{
alert("foobar");
}
</script>
Dim strScript As String = "<script language='javascript' id='myClientScript'>foobar();</script>"
Page.RegisterStartupScript(“callTest”,strScript)
However, the Page.RegisterStartupScript seem to be working only under the Page_load function....
When I put it in a Sub, like this:
Sub Test
Dim strScript As String = "<script language='javascript' id='myClientScript'>foobar();</script>"
Page.RegisterStartupScript(“callTest”,strScript)
End Sub
This won't work. As I link the above function to an asp button. I triggered the button, but nothing happens. So is there anyway to trigger javaScript function conditionally from asp.net? From a Sub function instead of on every page_load?
Thanks!!
You should represent this line
Dim strScript As String = "<script language='javascript' id='myClientScript'>foobar();</script>"
as
Dim strScript As String
strScript = "<script language='javascript' id='myClientScript'>foobar();</script>"
If you want to call javascript for asp.net button click, you can use OnClientClick attribute with a client side function OnClientClick="buttonClick()":-
<asp:button id="Button1"
runat="server"
OnClientClick="buttonClick()"
Text="Click!" />
The javascript injected using the RegisterStartUpscript method is executed when the page is first loaded. Try using the RegisterClientScriptBlock method when you need to inject and execute javascript after the
Type t = this.GetType();
if (!ClientScript.IsClientScriptBlockRegistered(t, "myClientScript"))
{
ClientScript.RegisterClientScriptBlock(t,"myClientScript", sb.ToString());
}
Not sure if it will work inside an updatepanel though
Below is the JavaScript function I am using and have used on numerous web applications where I work. On this specific example, I am comparing the clients IP address to a predetermined IP address on our network, and if the clients IP doesn't match, I display a message box that then informs the user that their specific computer terminal is not permitted to make these specific requests.
Dim ipAdd as string = nothing
ipAdd = Request.ServerVariables.Item("REMOTE_ADDR")
If ipAdd <> "###.###.###.###" then
ClientScript.RegisterClientScriptBlock(Page.GetType, "Script", "<script language='javascript'>alert('This terminal is not permitted to submit requests.');</script>")
End If

IHttpHandler for images producing a stackoverflow in IE

I have a directory of images that reside outside the context of my web application that I need to serve to the user. Currently I'm using an IHttpHandler to serve the images and using some javascript to navigate through a set of images (the navigation is primitive for now). I followed examples for using IHttpHandler to serve images closely but when I view the images in firefox the browser hangs and when I view in IE I get a "Stack overflow at line: 0".
Code for the IHttpHandler
Public Class ShowImage : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) _
Implements IHttpHandler.ProcessRequest
Dim picid As String
If context.Request.QueryString("id") IsNot Nothing Then
picid = context.Request.QueryString("id")
Else
Throw New ArgumentException("No parameter specified")
End If
'' Convert Byte[] to Bitmap
context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
context.Response.Cache.SetNoStore()
context.Response.Cache.SetExpires(DateTime.MinValue)
Dim newBmp As Bitmap = GetPhoto(picid)
If newBmp IsNot Nothing Then
Dim imgGraphics As Graphics = Graphics.FromImage(newBmp)
imgGraphics.DrawImageUnscaled(newBmp, 0, 0, 640, 480)
context.Response.StatusCode = 200
context.Response.ContentType = "image/jpeg"
newBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg)
newBmp.Dispose()
Else
'' Return 404
context.Response.StatusCode = 404
context.Response.End()
End If
End Sub
...
Public ReadOnly Property IsReusable() As Boolean _
Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
End Class
Here is the javascript code that's calling the IHttpHandler defined above:
function updateImage(){
var ddlPhotos = document.getElementById("ddlPhotos");
var selected = ddlPhotos.options[ddlPhotos.selectedIndex].value;
if( selected != -1 ){
// Update the image
retrievePicture(document.getElementById("propertyImage"), selected)
}
}
function retrievePicture(imgCtrl, picid)
{
imgCtrl.src = 'ShowImage.ashx?id=' + picid;
}
Finally here's the img tag that is the "place holder":
<img src="#"
alt="Property Photo"
width="640px"
height="480px"
id="propertyImage"
onload="retrievePicture(this, '<%= pictureId.value %>');"
/>
I'm confused as to why the javascript seems to spiral out of control...
My guess - not being a JavaScript expert - is that the onload event is triggered any time the image finishes loading. In other words, as soon as the image is loaded, it triggers loading a new one... which triggers loading a new one... which triggers loading a new one etc.
You will probably be able to see that in multiple calls to the server for the same image - unless the browser is caching it, of course. Anyway, you'll either need to trigger it in some other way, or make the trigger detect that the image which has been loaded is already the right one, and there's no need to replace it.
I suspect the act of changing the src and loading a new image may be triggering the "onload" event of the image again.
Try clearing the event before setting the source, will probably look similar to this:
function retrievePicture(imgCtrl, picid)
{
imgCtrl.onload = null;
imgCtrl.src = 'ShowImage.ashx?id=' + picid;
}

Resources