3

I have a web application which is inside another web application's iframe (both are done in ASP.NET MVC).

Is possible to read from the application inside the iframe the query string parameters from the parent web application using C#?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Catalin
  • 11,503
  • 19
  • 74
  • 147
  • Please add information on domain (url) of each involved page - feels like you may be trying to work around same origin policy restriction using wrong methods... – Alexei Levenkov Apr 10 '13 at 07:00

3 Answers3

5

You have to append whatever data you want to read from parent page from the page within iframe to the iframe url itself. For example:

<iframe id="yourid" src="yourpage.aspx?parentdata=whatever_data_to_be_captured">
</iframe>

Now, you have to parse the src value from your iframe page to capture data.

This is a trick, and as far as I know there seems to be no direct way to access parent page data from iframe.

sandip
  • 70
  • 4
  • I was thinking about the same thing also, but i wanted to know if there is another way of doing this without injecting the parameters inside the iframe src property. – Catalin Apr 10 '13 at 07:15
  • No, there is no other way, else it would be a security hazzard. – sandip Apr 10 '13 at 10:07
1

window.parent should give you a reference to the Parent.There is also a window.top which gives the immediate parent so if your application is nested more than once you should use the right property.

From there you can do window.parent.location (or window.top.location) which has various properties of the URL requested.

To actually read the query string there are posts about that here. You just need to replace window.location with window.parent or window.top :

See the answer here : How to get the query string by javascript? and much more detailed answers here: How can I get query string values in JavaScript?


In the Controller : Since you said you need the query strings in the controller. You'll need to still get the query string like I've described above, then you'll pass it into the controller by putting it in the call to the src of the inner frame.

<iframe src=""></iframe>

It all depends on how and when you're setting the src attribute of your inner frame. You need to somehow change it at some point.

From your outer frame you could do:

document.getElementById('myframe').src
 = "http://innerApplication/innerController/actionmethod/?" 
   + window.location.search;

Or, from inside the frame itself, you'll get the query string first, then do a redirect/reload by doing window.[top/parent].location = the url + query string

Then you'll read the query string as if it were the query string of the inner application.

Community
  • 1
  • 1
gideon
  • 19,329
  • 11
  • 72
  • 113
  • I need to do this from code behind (before i render the page to have access to javascript) – Catalin Apr 10 '13 at 06:33
  • By code behind you mean controller? You need to know the query string parameters in the controller? Then in the inner frame you need to make a request to the controller and pass the query string as a parameter to the controller from javascript, and you'll get the query string in javascript like I've described – gideon Apr 10 '13 at 06:37
  • Yes, exactly, i need to access the query strings from controller before i render the html page – Catalin Apr 10 '13 at 06:38
0

You should use %26 instead of & to send to iframe, for instance:

Link : 2.asp?ppp=/Payam.asp?sid=1%26PID=1&sa=12313 <br>

In code : when I requset.queryString("ppp") in the parent it shows: /Payam.asp?sid=1&PID=1

You can use this as source of your iframe, it has two parameters:

<IFRAME name="My"  src='<%=ppp %>'></IFRAME> 
Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63