Solved

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

Posted on 2009-06-30
9
909 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

789 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