Solved

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

Posted on 2009-06-30
9
901 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 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

20 Experts available now in Live!

Get 1:1 Help Now