Thursday, November 17, 2011

Get the file name from browser url using c#

In previous post we have seen how to pass java script variables to serverside.In this posti wll show how to get the browser information using csharp.Comparing to javascript there is no direct piece of code to get the file name with out extension in code behind using csharp.For this first we get the Absolute path from url then GetFileNameWithoutExtension property is used to remove the extension from path.using Here i have shown to get the file name using path in csharp

Code Behind:
string urlname=Request.Url.AbsolutePath;
string filename= System.IO.Path.GetFileNameWithoutExtension(urlname);
Java Script:
var fname= window.location.href;
var fname=window.location.href.substr(window.location.href.lastIndexOf("/") + 1); 
The sub string is used to get the file name with out extension.For example if path of the page is "defalut.aspx" then the output will be "Default"
If you want to get the raw url of from browser ,you need to write the below line of code.
 string Rawurlmain = HttpContext.Current.Request.RawUrl;
 string  Substringurl = urlmain.Substring(Rawurlmain .LastIndexOf('=') + 1);
Post a Comment

Bel