Jeremy Wadsworth
My Contribution to the ASP.NET Community


Welcome   
Welcome to my personal website. Here you will find blog posts related to web development as well as personal related interests. If you're here for the source code to my Personal Web Site Kit, please register and you'll receive an email when your account has been approved.


Latest Blog Entries

 Wednesday, May 09, 2007


Get the file name of the current page in ASP.NET    

Here is a nice piece of code to get the file name of the current page that is being viewed. For the purpose of illustration, I'm using some altered code from one of my web applications. This code is being used in the master page to set a session variable only when I'm on a certain content page.

If System.IO.Path.GetFileName(HttpContext.Current.Request.FilePath).ToLower = "secure.aspx" Then
 Session.Item("MyVariable") = "window_onload();"
Else
 Session.Item("MyVarible") = ""
End If

If I'm on any other page, I want the session variable cleared.


jeremy at 1:34 PM | (32) Comments | Add a comment | Permalink





Commments
Anthony Peluso said...

Thanks for this!

Sunday, July 15, 2007 3:53 PM

Derick said...

Nice, thanks.

Saturday, August 04, 2007 6:24 PM

rana said...

Try this, it will give you the current webpage name. System.IO.Path.GetFileName(System.Web.HttpContext. Current.Request.Url.Absolu tePath

Wednesday, September 26, 2007 1:29 PM

Amar said...

Nice code .......

Wednesday, October 17, 2007 7:44 AM

NeAl said...

Thank you!

Tuesday, November 20, 2007 9:32 AM

Viktar said...

It's going to return full path from the root of website. For example: /admin/mypage.aspx

Thursday, January 31, 2008 9:18 AM

Viktar said...

Use this to get file name: Request.FilePath.Remove(0,Request.FilePath.LastIndexOf("/") 1) Don't forget to do ToLower() if you comparing it to something.

Thursday, January 31, 2008 9:27 AM

Cerise said...

Thanks!

Friday, March 21, 2008 10:52 AM

peter said...

don't know if you will see this but you forgot a sign Request.FilePath.Remove(0,Request.FilePath.LastIndexOf("/") 1)

Thursday, March 27, 2008 5:44 AM

jeremy said...

Peter, The email I received has the plus sign. It appears my filter removes the plus sign.

Saturday, April 05, 2008 10:46 AM

Liam said...

Cheers, found this on google and exacly what im after :)

Thursday, August 07, 2008 8:41 AM

snang said...

Likewise, this code was a life saver. Well, actually the code in the 3rd comment.

Sunday, August 10, 2008 9:43 PM

Glen said...

System.IO.Path.GetFileNameWithoutExtension(System.Web.HttpContext.Current.Request.Url.AbsolutePath)

Wednesday, November 12, 2008 3:39 PM

ricko said...

I prefer the Remove-LastIndexOf solution, rather than getting IO path, it must be more efficent.

Wednesday, January 28, 2009 4:31 PM

Nripin Babu said...

Lovely Code in there Cheers!!!

Wednesday, February 11, 2009 9:40 PM

kathirvel said...

string url=Request.Url.AbsolutePath.ToString();

Wednesday, April 01, 2009 10:14 PM

Tony Mathew said...

Using IO at a web layer is not a good approach. Rather try this private string GetCurrentPage() { Uri uri = Request.Url; string[] segment = uri.Segments; string page = string.Empty; if( 0 < segment.Length ) { page = segment[segment.Length - 1]; } return page; }

Thursday, May 14, 2009 1:08 PM

MA said...

Thank you. It helped in time

Monday, August 24, 2009 8:20 PM

Igor said...

Will not work if there was Server.Transfer, or PostBackUrl of a button is defines

Friday, January 15, 2010 10:35 AM

Paul said...

Pardon my VB, but by riffing on Tony's method, one could do this: Protected Function getCurrentPageName() As String Return Request.Url.Segments(Request.Url.Segments.Length - 1) End Function

Friday, May 07, 2010 11:26 AM

Tiffany jewellery said...

I prefer the Remove-LastIndexOf solution, rather than getting IO path, it must be more efficent.

Friday, July 16, 2010 9:00 PM

replica watches said...

Always good to see, this was obvious a excellent post. In theory would like to be such a good writer too. You need time to creat that brilliant and in addition real effort to create a excellent article.

Tuesday, August 10, 2010 5:06 AM

mbt shoes clearance said...

It's a precise and informative article. Things are formed symptom less. Get to know lot's of artifact which were unbeknownst to me. It's truly one of the most facilitative article I get ever read. Thanks for to percentage sensing for author from you. Symmetrical I bookmarked this diplomat as asymptomatic for to get supply from it in incoming.

Thursday, August 19, 2010 4:18 AM

gucci wallet said...

wow, cool, yep this is a great site. I enjoy.

Saturday, August 21, 2010 7:35 AM

Invest in UK said...

The person who create this post he is a great human..thanks for shared this with us.i found this informative and interesting blog so i think so its very useful and knowledgeable....

Sunday, August 22, 2010 11:54 AM

fdsf said...

They are designed to hamper the virus' replication in cells, thus buying time for the immune system to mount <a href="http://www.watchsever.com" title="replica watches">replica watches</a> a response replica watches and crush the invader. The research was conducted by the US Army Medical Research Institute <a href="http://www.anymine.com" title="replica watches">replica watches</a> of Infectious replica watches Diseases in collaboration with a Washington-based biotech firm, AVI BioPharma, and was revealed <a href="http://www.bagsabc.com" title="replica handbags">replica handbags</a> in the journal replica handbags Nature Medicine. The Pentagon pumped funding into research for a vaccine and treatment for Ebola-type viruses in the wake of the September 9, 2001, terror attacks on the United States. For filoviruses are on the list of <a href="http://www.bagsabc.com/louis-vuitton-handbags.html" title="Louis Vuitton">Louis Vuitton</a> pathogens such Louis Vuitton as anthrax that are considered tempting sources for biological warfare or terrorism. An important step in combatting <a href="http://www.someboots.com" title="ugg boots">ugg boots</a> Ebola was ugg boots announced in May this year, again using tests on lab monkeys but involving a somewhat different technique to disrupt viral replication.

Tuesday, August 24, 2010 1:12 AM

anna said...

Hey , your information is very helpfull .Add me into your newsletter!

Tuesday, August 24, 2010 5:14 AM

replica watches said...

Lovely Code in there Cheers!!!

Wednesday, August 25, 2010 11:45 AM

coach bags said...

Well said <a href="http://www.coach-bags-outlet.com/">coach bags</a>. I never thought <a href="http://www.coach-bags-outlet.com/">coach bags outlet</a> would agree with this <a href="http://www.coach-bags-outlet.com/">coach outlet online</a> opinion, but I'm starting to <a href="http://www.chanelbags2010.com/">chanel bags 2010</a> view things from a different view. I have to research <a href="http://www.chanelbags2010.com/">chanel bags</a> more on this as it seems very interesting <a href="http://www.chanelbags2010.com/">chanel bags online</a>. One thing I don't understand though is how e<a href="http://www.chanelbags2010.com/">cheap chanel bags</a> is related together.<a href="http://www.chanelbags2010.com/">chanel outlet store</a>Actress very beautiful, very good <a href="http://www.chanelbags2010.com/">chanel bags outlet</a>. zocy005 on 2010-8-27

Friday, August 27, 2010 4:22 AM

wholesale nfl jerseys said...

Hey guys, I hope this allowed, I have never used this website before so I wasn't really sure what this was going to do. <a href="http://www.nfljerseyse.com" title="wholesale nfl jerseys">wholesale nfl jerseys</a> So this is just a test post. I really like this forum, it has some excellent discussions that take place.

Thursday, September 02, 2010 3:20 AM

home cctv said...

These are really basic instructions…but still important.really its very useful and informative.its good tips and easy steps. so thanks for nice post.

Saturday, September 04, 2010 6:43 AM

aliza said...

[url=http://www.dvdsuperdeal.com/dexter-seasons-1-4-dvd-boxset.html]Dexter DVD 1-4[/url] [url=http://www.dvdsuperdeal.com/lost-seasons-1-6-dvd-boxset.html]Lost DVD 1-6[/url] [url=http://www.dvdsuperdeal.com/family-guy-seasons-1-8-dvd-boxset.html]Family Guy DVD 1-8[/url] [url=http://www.dvdsuperdeal.com/greys-anatomy-seasons-1-6-dvd-boxset.html]Grey’s Anatomy DVD 1-6[/url] [url=http://www.dvdsuperdeal.com/criminal-minds-seasons-1-5-dvd-boxset-1.html]Criminal Minds DVD 1-5[/url] [url=http://www.dvdsuperdeal.com/zumba-fitness-dvd-boxset.html]Zumba DVD[/url]

Monday, September 06, 2010 3:28 AM

  Leave a comment

Enter Your Name:
 
Enter your website
Security code from image below:  
Need Custom Controls Work or Training?
   


Join WebHost4Life.com






If you would like to help support this project, please click the button below to make a small donation.