Solved

How to tell if ASP.NET app is running inside a frame; using c#, not javascript

Posted on 2009-06-30
9
897 Views
Last Modified: 2013-11-07
I need to be able to determine if my c# ASP.Net application is inside a HTML frame.  In Javascript, I would normally use:
if (top.location != location)

How can I do this in the code-behind of a user control?
0
Comment
Question by:alien_technology
  • 5
  • 4
9 Comments
 
LVL 3

Expert Comment

by:eirikurh
ID: 24749938
If your mainpage have a default.aspx as your mainpage, and your frame have another url like "main.aspx"
then you can check the url on the server side:
if (Request.Url.Segments.Length > 2)
{
  if (Request.Url.Segments[2].ToLower(CultureInfo.InvariantCulture) != "default.aspx")
  {
     //in Frame
  }
}
0
 

Author Comment

by:alien_technology
ID: 24750080
Thanks for the reply eirikurh, but I don't think that it does what I need.  

It appears that in your example, if main.aspx is loaded by itself, Request.Url.Segments[2] will be "main.aspx".  If main.aspx is loaded inside an iframe, Request.Url.Segments[2] will still be "main.aspx", won't it?  The page main.aspx still does not know that it was loaded inside a frame.
0
 
LVL 3

Expert Comment

by:eirikurh
ID: 24750430
See Request Referrer points to default.aspx when main.aspx is loaded in frame.
0
 

Author Comment

by:alien_technology
ID: 24754937
But if a link in main.aspx does a postback or links to another page and then back, referrer would no longer point to default.aspx.  

Is there no .Net equivalent to Javascript's top.location?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:eirikurh
ID: 24757235
Do you have control over the frame page.

If i were you, i would add a parameter to the frame.

src="main.aspx?inframe=1
0
 

Author Comment

by:alien_technology
ID: 24757457
I could, but that would mean that the querystring would have to be updated for all links on all pages in the frame.  

Is there no way for a .Net page to access the parent frame to get the URL?  That would definitively indicate if it is in a frame.
0
 
LVL 3

Expert Comment

by:eirikurh
ID: 24759322
The server have no access to the Browsers DOM
Why do you need to check if the page is loaded in frame?
0
 

Author Comment

by:alien_technology
ID: 24798588
We are migrating applications that used to be stand-alone to now work in an iframe in DotNetNuke.  With a single code base, we want to display the header images and menuing if the app is on its own, but remove it if its in a DNN window since DNN can display that stuff.
0
 
LVL 3

Accepted Solution

by:
eirikurh earned 500 total points
ID: 24809963
You could make a cookie on the client side
and read it later on server side

---Client side ---
var seconds = 60;
var date = new Date();
date.setTime(date.getTime() + (seconds*1000)); //1 minute expire
var expires = "; expires=" + date.toGMTString();
var framevalue = '0';
if (top.location != location)
{
  framevalue = '1';  
}
document.cookie = 'inframe'+"="+framevalue+expires+"; path=/";


--- Server Side ---
bool isFrame = false;
if (Response.Cookies["inframe"] != null)
{
  string cookieValue = Response.Cookies["inframe"].Value;
  if (cookieValue.Equals("1"))
    isFrame = true;
}
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now