Link to home
Start Free TrialLog in
Avatar of michelle0
michelle0

asked on

ASPX to delete itself on Page_Unload

W2003 Server
IIS 6
VS 2005
ASP.NET 2.0

I need to Delete an ASPX page from the server when it is unloaded.

Looking for 1 of 2 solutions:

1) Set the page to delete itself when it unloads

OR

2) Somehow in the web.config for where these tmp files are created, set directory to delete any ASPX file when it is unloaded.

BTW, then page is not statically named.  It is <sessionid>.aspx

TIA,
Ken



Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Hi michelle0,

This seems like a very strange request... but the only place I can think to put this code would be in a Session_End (in the Global.asax file)

Session_End:
System.IO.File.Delete(Session.SessionID & ".aspx")

HTH.
-- Jason
Avatar of michelle0
michelle0

ASKER

Yes, strange, but with reasons that would take forever for me to post.

Thanks for your idea, but I don't want the session to end to delete the file.  I only want the TEMPORARY page to be served while the user is using it.  When they leave the page, then I need it deleted.

Any other ideas?

Thanks,
Ken
How would a user leave this page? Through a link to another page on the site or when a form is submitted?
both  and/or
This is a pretty crude solution. But you create a user control and drop it in every page that the temporary aspx page links to. The user control's code behind would retrieve the last aspx page it came from - HTTP_REFERER and then delete the file. Dont know if this would work for you?
michelle0,

Can you explain a little more how the <sessionid>.aspx file is being created and called?

-- Jason

Ok, I didn't want to get into this, but here it goes.

We have a legacy ISAPI that takes an ASPX file and merges several things into it an outputs the result as an encoded stream.

So, we use an ASPX file (i.e. MAIN.ASPX) to call the ISAPI dll and receive the response using HttpWebRequest.  Now we have a full ASPX file in an HtmlTextWriter.


We then write the text out to a temporary aspx file in an asptmps directory using a unique file name to the logged in user (either sessionID or a newguid()).  We then execute this page within the MAIN.ASPX using Server.Execute.

Now, this works great, but with only one problem.  If we have a server Web Form (<form runat=server>), then the Server.Execute compilation of the temp page causes the form action to think that it should refer to itself (asptmps/<ses or guid>.aspx).  I need it to assume postbacks to MAIN.ASPX and not the temp file.

So, as a workaraound, I thought that I would let it use the temp file for postbacks , but I would need to delete the temp file before the user calls or posts to the MAIN.ASPX again for a different merge (by menu maybe).  Otherwise, the SAME temp gets processed again before overwritten by the new merge.


Hope this makes sense.   Now, the ULTIMATE solution would be to execute (compile) the ISAPI's resulting code WITHOUT creating the ASPX file.  If someone knows how to do that, then I would create 4 blank 500 point questions and give them all to YOU!

Otherwise, I simply need to delete the file as the page unloads and before the Main.aspx runs again.
A little claarification:


Now, the ULTIMATE solution would be to execute (compile, parse and execute) the ISAPI's resulting code WITHOUT creating the ASPX file.
Wow am I glad I am not on that project :)...

How about after the page has returned to the Main.aspx on the page_load of that page it deletes the page?

I don't think you can delete a page from itself. It will tell you that page is in use...
ASKER CERTIFIED SOLUTION
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am going to use the Page_Unload because the aspx file has already been compiled and stored, so the PHYSICAL file would not be in use any longer.

Thanks