Classic ASP page loading twice per request - asp-classic

I have this very straight forward and simple script in classic ASP using Jscript as the scripting language:
var counter_value = Session.Contents.Item("counter") || 0;
Response.Write("old:" + counter_value);
Session.Contents("counter") = counter_value + 1;
Response.Write("<br/>");
counter_value = Session.Contents.Item("counter");
Response.Write("new: " + counter_value);
When this script is run in the browser for the first time I get this output:
old: 0
new: 1
But when I refresh the page in the browser, I get an unexpected result:
old: 2
new: 3
Why is this happening?

Found out that the problem comes from the page being executed twice per request Because of a rule in url-rewrite.
The rule states that all requests should be rewritten to index.asp, so when the browser sends a request, the content of index.asp gets returned in the response.
The same thing happens again when the browser automatically asks for a favicon.ico image.

Related

What is this redirection called and how can it be setup for an ASP based site?

Does this redirection method have a specific name, and how do I set it up for an ASP based site?
http://www.example.com/?URL=www.redirecteddomain.com
Ok, in that case, it not really the web server, but simply your code that can do this.
so, if you web page was:
http://localhost/MyJumpPage.aspx?URL=www.google.com
So, in your code, all you have to do is grab that 1st parameter, and then run code to jump/navigate to that page.
EG:
string strURL = "";
strURL = Request.QueryString("URL");
Response.Redirect("http://" + strURL);
So, the code behind a button, or even on page load can simply pull the query value from the url string, and then jump to that URL.

Why google login redirect back to site with hashtag - can it be avoided

I didn't succeeded to find any relevant answer to this so I must ask :)
I implemented google plus login to my site. With a few workarounds it work fine but...
When I am redirected back to my site from google I am returned to the following URL:
http://localhost/mysite/west/Default.aspx#state=/profile&access_token=ya29.qQDrtcVtgOEbS86Bg10puFG3dksJz74BlrEGulHldlJW2o5qQ6g7ilF17zQsm8iMLG0C82PQyp2Z-g&token_type=Bearer&expires_in=3600
Because of this parameter here #state=/profile& I first have to read URL in javascript on load, remove this part to fix URL and then do this:
var url = "Default.aspx?" + queryString;
window.location = url;
And then I can continue to read query string normally in code.
I don't like this because when I do this I make two postback on page and I want to avoid this if possible.
Is this redirect url must have this or this can be avoided?
Redirect to google:
string url = "https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email&state=%2Fprofile&redirect_uri="+this.Return_url+"&response_type=token&client_id=" + this.Client_ID;
Response.Redirect(url);
When I get back to my site:
if (this.Request.QueryString["access_token"] != null)
{
String URI = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + Request.QueryString["access_token"].ToString();
WebClient webClient = new WebClient();
Stream stream = webClient.OpenRead(URI);
Change response_type=token to response_type=code. The callback URL will then have a code query parameter which is accessible server side instead of a fragment. You will then have to implement step 4 to exchange the code for an access_token.

Single Page Applications using ASP Web Forms

I need to implement single page applications using ASP Web Forms. I faced with a navigation problem. I need to use a navigation pattern like this:
http:// web site url / ... / page.aspx? {query string} # {ListId} / {ItemId}
When a user request a data from the server, the request on the server doesn't contain hash # (because this is a client-side feature). And it looks like this:
http:// web site url / ... / page.aspx? {query string}
So, actually I need two requests:
to get a page without hash and load javascript;
to handle hash data using javascript and async call required data from the server.
Is it possible to implement this logic with only one request?
Are there any best practices?
You can append ListId/ItemId to query string before sending request and read it regularly on a server.
var url = 'http://example.com?param1=10&param2=20#1000';
var beforeHash = url.split('#')[0];
var itemId= url.split('#')[1];
var processedUrl = beforeHash + '&itemId=' + itemId;
If your request is not already fired from JavaScript, you will have to hook into link's click event...
Or maybe you can get rid of # entirely and scroll content via JavaScript (my guess is that you use # because of local anchors to jump to different places in document)?
BTW There is window.location.hash property.
Update:
Based on your comment the flow is like this:
User types URL with #ItemId
Server returns the page
JavaScript reads #ItemId from window.location, puts it into QueryString and makes a request
Server returns the page based on modified QueryString
In this situation the two-requests pattern seems to be the only viable option. By design server does not get #Item part (called fragment). So there is no way to guess ItemId upon initial request. If after second (ajax) request, you refresh #ItemId dependant parts of the page through JavaScirpt, user experience will not be hindered much.

asp.net mvc 3 - redirect to external url adds +

So I need the following:
When someone navigates to a certain page then it checks if it's the facebook Like crawler. If it's true then it shows the page normally (with the opengraph tags and everything).
If it's false (so the user is just a normal user) it should redirect to a specific external URL.
This is my code so far:
NotebookModel notebookmodel = db.NotebookModels.Find(id);
var isFacebook = Request.UserAgent != null && Request.UserAgent.Contains(Config.FacebookUA);
if (!isFacebook)
{
return Redirect(notebookmodel.Url);
}
return View(notebookmodel);
This works perfectly except for 1 small detail (that does mess up my result): it appends a "+" at the end of my external url once redirected. The url in notebookmodel.Url is right, but once it passed through the Redirect it appends a "+" symbol at the end. Anything I'm doing wrong or can do to fix this?
I suspect that you have a whitespace at the end of the notebookmodel.Url property. Spaces are converted to + when url encoded. You could Trim it.

How should i write Response.Redirect() in asp.net

In asp.net two overload method of Response.Redirect() exist.
Public Sub Redirect ( _ url As String )
Public Sub Redirect ( _ url As String, _ endResponse As Boolean _ )
I would like to know the diffrence between these two? and which one should i use?
The first overload redirects to another URL, the second allows you to say whether the current code should continue to execute e.g. if Response.Redirect("http://philippursglove.com", True) occurs in the middle of a block of code, the rest of the block of code will keep executing and run database updates or whatever.
As to which one you should be using, we can't tell you without seeing it in the context of a bit more of your code.
Also have a look at Server.Transfer, which achieves much the same thing as Response.Redirect but without sending anything to the browser, which can take a bit of pressure off your Web server. See Server.Transfer vs Response.Redirect.
They both send your browser a 302 response telling it to request the specified page. You usually don't want the response to continue if you are redirecting someone to a new page so by default that is what Response.Redirect("/") does.
If you do want to continue processing a response though you will need to set the second parameter to false.
So in this example a will be 1:
var a = 1;
Response.Redirect("/aboutus.aspx");
a = 2;
In this example a will be 2 because the thread keeps running after the redirect.
var a = 1;
Response.Redirect("/aboutus.aspx", false);
a = 2;
Careful though if using this in a try catch. A slight oddity means that in the next example a will be 2!
var a = 1;
try
{
Response.Redirect("/aboutus.aspx");
}
finally
{
a = 2;
}

Resources