0

I might be approaching this issue the wrong way, but I am in need of knowing either the current URL of the current ASP.NET page or the form name.

I have a C# application with a WebBrowser which runs the ASP.NET application. Whenever I check the current URL in the WebBrowser component it always says it's the 'Default.aspx' page. Which is fine when you're running it in another browser, but I am in need of knowing the exact URL or the form.

I've also tried creating a javascript function to return the current URL, but since this function is only added to the 'default.aspx' page (so all the pages can access this function) I only get returned the 'default.aspx' URL.

Any ideas?

Thanks!

Edit: I forgot to mention that I use WCF events binded through net.tcp. When the ASP.NET application receives such an event, it should handle differently based on the current page it's on. I am not able to use Request or HTTPContext.Current, since there are NULL is my case.

user1782815
  • 195
  • 1
  • 3
  • 17
  • http://stackoverflow.com/questions/96029/get-url-of-asp-net-page-in-code-behind – CodingDefined Aug 01 '14 at 09:35
  • Thanks, I updated my question. These won't work. – user1782815 Aug 01 '14 at 09:48
  • 1
    Why don't you put your script file in the main js file. Instead of Default.aspx. Place that in your main js file. I hope all your pages or any master page will be having a reference to it. – Shah Aug 01 '14 at 10:08
  • what about System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString; – CodingDefined Aug 01 '14 at 10:08
  • I'm not using WebOperationContext, but InstanceContext. This has no URI. For the .js file, that might be a good way to go. Although I tried adding the following in the master page: I called the function GetUrl, and tried calling this from my C# WebBrowser component - .Document.InvokeScript("GetUrl");. No luck though. – user1782815 Aug 01 '14 at 10:31

1 Answers1

0

Try below

private string GetCurrentURL()
{
  string appPath = Request.ApplicationPath.ToString();
  string[] strPath = Request.RawUrl.ToString().Split('/');
  if (strPath.Count() == 2)
  {
      return Request.RawUrl.ToString().Replace(appPath, "")
  }
  else
  {
     return Request.RawUrl.ToString().Replace(appPath, "").Substring(1));
  }
}