Response.Redirect causes download of the swf - c#

I have a flash image slider with a button below each image.
When i press that button, the user is redirected to a new page
where i add that image product to my cart.
The problem is that after doing the adding, i want to redirect the user back to the initial page.
The code:
protected void Page_Load(object sender, EventArgs e)
{
addProductToBasket(getCategoryIdFromUrl(), getProductIdFromUrl());
Response.Redirect(Request.UrlReferrer.ToString());
}
Please note that in Firefox is working fine but in IE or Chrome it is DOWNLOADING the swf...If i comment Response.Redict(...) the user remains on this page so the click button is working well, only the redirect seems to be the problem.
Any suggestions please?
Edit: The problem seems to be that Request.UrlReferrer keeps as link not the initial page containing the swf but the swf itself....
So, instead of doing redirect to:
http://localhost:1336/Site/Index.aspx
if does redirect to the swf contained on the Index.aspx page
http://localhost:1336/carousel/carouse.swf
Solved: with a session variable where i keep the initial page's url

It seems to me that it's a function of the flash player setting the referrer header differently in different browsers.
If that is the case, then you might want to have the flash player get the url of the page it is hosted on, and pass that as a parameter to your page, and then redirect to the contents of the parameter.
What I think you really should be doing though is registering an HttpHandler (IHttpHandler implementation) for the URL which performs the processing and creating a response which returns JSON or XML, which flash can easily parse.
This way, you don't have a page reload and you have a seamless experience.

Related

how to call one webpage from another webpage in asp.net

I have 2 web applications. webapp1 is running at location say - weblocationlocation1/webapp1/default.aspx
and webapp2 is running at different location say -
weblocationlocation2/webapp2/default.aspx
Now, If I want to call webapp2/default.aspx from webapp1 then how to call.
how to run Page_Load(object sender, EventArgs e) of webapp1 from webapp2/default.aspx.
I have to stay on webapp1/default.aspx in my browser. and still want to load webapp2/default.aspx (ONLY from my code of button clicked). in this case, how to store cookie/session variables. and want to maintain them in webapp1 across all pages.
If you want to do this via a redirect then:
Response.Redirect("weblocationlocation2/webapp2/default.aspx");
Or directly on the server use
Server.Transfer("weblocationlocation2/webapp2/default.aspx");
Or
Server.Execute("weblocationlocation2/webapp2/default.aspx");
The last will return control to the calling method (the second won't).
as described by # Justin Harvey you can use Page_load() method and call Response.redirect method to redirect to your desired web page
You can also use javascript if you want to redirect to your page on event such as button on click
for that you can do following
btn_demo_onClick()
{
window.location = "abc.aspx";
}
it just a complementary option if you want to go with javascript
Thanks
Response.Redirect("default.aspx"); // At URL You will Get the default page as what you are redirecting to.
Server.Transfer("default.aspx"); // At URL You will not Get the default page as what you are redirecting to.
example : If you are logged in Login page then you want to redirect to default page ,then you can use both the above mentioned methods.

How do I fully refresh an ASP.NET page so that Page_Load is run again?

I have an ASP.NET page where an iframe src is being dynamically set:
<iframe id="specialframe" src="<%= IframeSrc %>"></iframe>
And the codebehind:
internal string IframeSrc { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
IframeSrc = SpecialService.GetNewUrl();
}
SpecialService is a third-party service that, when called, returns a one-time use URL that looks something like:
http://www.specialservice.com/oneTimeUseId=fh8396123lkjufgh49
For security purposes, this ID can only be used once before expiring. However, this leads to a problem.
The first time I display the page in a browser, it loads the iframe correctly. However, if I hit refresh, I will receive an error message in the iframe because it's trying to use the same one-time use ID. It works properly if I Ctrl+F5 refresh to force a cache clear, but I can't tell my users to do that every time.
My question is, is it possible to force the browser to never use the cache and always request a fresh copy from the server?
You need to set expiration and caching one your page correctly so browser always will have to request page from the server.
This question maybe a good start http://msdn.microsoft.com/en-us/library/y18he7cw.aspx.
You need cacheability to none (and maybe to set expiration in the past, maybe SetMaxAge too):
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddMinutes(-30));

How to redirect from one ASP.NET page to another

How do I redirect from one ASP.NET page to another ("Webform2.aspx") by means of a button?
You can redirect from one page to another using Response.Redirect()
set PostBackUrl property of button, like this :
button1.PostBackUrl= "Webform2.aspx";
You can redirect to another ASP.NET page using the code below :
Response.Redirect("Webform.aspx");
This is the simplest way
Personally, if all you're wanting to do is load a new page when a button is clicked, I would do this with client-side script.
You could use a JS library for this (eg: jQuery), like so:
jQuery
$(function() {
$('#<%= button1.ClientID %>').click(function() {
window.location.href = "Webform2.aspx";
});
});
ASP.NET
<asp:Button id="button1" runat="server"/>
Or, for a specifically ASP.NETesque way to do it, you can use Button.PostBackUrl as Antonio suggests, which still uses client-side script but means you don't have to write it yourself. The HTML for the button renders as:
<input type="submit" name="button1" value="Button" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("button1", "", true, "", "webform2.aspx", false, false))" id="button1" />
If you've got other processing to do server-side and you need to redirect afterwards, use
Response.Redirect("Webform2.aspx"); in your click handler.
If that's not working for you, please add some more detail to your question to explain what's happening.
Well there are lot of ways. Response.Redirect, Server.Transfer, Javascript call to the page.
Javascript call is required when u have no server side actions for the button.
onclick="javascript:window.location.href = Webform2.aspx?id='<%=Request.QueryString["id"]%>'"
Server.Transfer will do a re-direct at server side. i.e, The browser will still show after the response from webform2. Webform1.aspx will re-direct the request to webform2 and webform2 will give the req. (Req = 1, Res = 1)
Response.Redirect: webform1 will send a response asking the browser to make a new request to webform2. In this case, the browser will change the url as it is making a new req to webform2.(Req = 1 + 1, Res = 1+1)
There is one more way, form.submit() if you are interested. The traditional html form submit.
Forgot to mention the best of all, the cross-page postback with PostBack url..
http://aspdotnetcode.source-of-humor.com/TipsAndTricks/General/CrossPagePostbackAspNetCrossPagePostback.aspx
You can use below code :
protected void Button1_Click(object sender, EventArgs e) {
Response.Redirect("default2.aspx");
}
Notice that default2.aspx is your second web page name and you
Response.Redirect(string url) issues a 302 HTTP status code instructing the client to redirect to url. The browser will issue a new request for url and the URL will change in the address bar.
Server.Transfer(string path) terminates execution of the current page and starts execution of a new page on the specified path i.e. internally within IIS. Therefore the URL in the browser address bar will not be changed. The page you transfer to must be an aspx page in the same web site.
The differences are subtle but important. A simple way to think about this is to ask yourself "should the user bookmark/favorite this URL?". Use Response.Redirect if the URL has changed and future visits to the content should be on the new URL. Use Server.Transfer if the URL is correct and current but you need to display different content this one time - maybe you are displaying an error message or you need the user to enter their credentials to continue or there is some other reason why the content should change but the URL should not.
Either of the above can be used within the Click event handler of an ASP.NET Button control in your code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Webform2.aspx");
// OR
Server.Transfer("Webform2.aspx");
}
Both Response.Redirect and Server.Transfer methods are used to transfer a user from one web page to another web page. Both methods are used for the same purpose but still there are some differences as follows.
The Response.Redirect method redirects a request to a new URL and specifies the new URL while the Server.Transfer method for the current request, terminates execution of the current page and starts execution of a new page using the specified URL path of the page.
Both Response.Redirect and Server.Transfer has same syntax like:
Response.Redirect("UserDetail.aspx");
Server.Transfer("UserDetail.aspx");
Before touching on more points I want to explain some HTTP status codes, these are important for the understanding of the basic differences between these two. The HTTP status codes are the codes that the Web server uses to communicate with the Web browser or user agent.
Response.Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. For example, suppose you are on the web page "UserRegister.aspx" page and it has a button that redirects you to the "UserDetail.aspx" web page.

HttpRedirect on every link within FBML application, why?

I have set up the SDK on my FB application but for the life of me cannot work out why the redirection happens.
The app is an IFrame so, for testing I have two pages, on page one a link to page two, when I click the link the whole page is redirecting as opposed to the IFrame src redirecting.
Both pages are checking to see if the user is logged in with the following code..
protected string requiredAppPermissions = "user_about_me,email";
protected FacebookApp fbApp;
protected CanvasAuthorizer authorizer;
protected void Page_Load(object sender, EventArgs e)
{
fbApp = new FacebookApp();
authorizer = new CanvasAuthorizer(fbApp);
authorizer.Perms = requiredAppPermissions;
if (authorizer.Authorize())
{
}
}
I have had a look in source and can see this in the FacebookAppRedirectHttpHandler, I just can't understand why you would want to keep redirecting the full page for every navigation link?
The most important reason is that Facebook passes the authentication to the signed_request to the source on every request. It either does this with a POST in the body or with a GET in the querystring. The reason we do this is because cookies aren't 100% reliable. If we redirected inside the iframe we would have to store the user's session in a cookie. Some browsers, including safari, don't let iframe apps create cookies. There are ways around this, but for most people the way we have it works best. If you want to have a redirect inside the iframe without changing the top url you will have to save the session in some way and pass it to the second page. You could do this by adding it to the querystring (complicated) or storing it in the Session (not very scalable) or using cookies (not reliable).

Button redirect error

I am trying to a redirect the user when they click on a specific button
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("ControlPanel/Default.aspx");
}
The problem is when I click Button1 it redirects me to another page
localhost:57988/WebSite5/Default.aspx
and the weirdest thing is it open another page with this link above, not the default page I have, but another but with the default.aspx page url that you see!
Any suggestions?
You are not doing a redirect, you are doing a transfer. That means that the execution continues with the new page, but the URL doesn't change. The page that you transferred to is returned as response to the request for the first page.
Use Response.Redirect instead of Server.Transfer to do a redirect.
When you use Server.Transfer it won't display the new URL in the query string. Could it be that is what's throwing you off?
Here is a good article on Response.Redirect vs Server.Transfer. If you really want to redirect the user then you should use Response.Redirect.

Categories

Resources