Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, December 1, 2010 1:27 AM
Suppose my website address is like http://localhost/MyWebsite
I wanted to access this url in Application_start event in global.asax, Can i some how get it?
[I know i can get it via application_beginrequest or other event]
All replies (17)
Tuesday, December 7, 2010 9:53 PM âś…Answered
Hello Kamran,
Thank you for your coming back.
According to the document (Request is not available in this context exception in Application_Start) in the post above, I agree that we may not able to get the Application URL in the application_start event for all the IIS servers in a unique way.
On the other hand, I think relying on applicationj_beginrequest event is a good approach. We can get the Application URL and then store it in somewhere, a static variable for example. So the code might like:
public static string ApplicationURL = null;
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (ApplicationURL == null)
{
// Only assign the ApplicationURL once.
// Remove the page path information.
Regex regex = new Regex("(" + HttpContext.Current.Request.Url.AbsolutePath + ")$");
ApplicationURL = regex.Replace(HttpContext.Current.Request.Url.AbsoluteUri, string.Empty);
}
}
If you still have problem, please feel free to let me know.
Wednesday, December 1, 2010 1:59 AM
try this:
HttpContext.Current.Request.Url.ToString()
Wednesday, December 1, 2010 2:07 AM
You can use VirtualPathUtility class. It has ToAbsolute() method.
string absolutePath = VirtualPathUtility.ToAbsolute("~/");
Regards,
Huske
Wednesday, December 1, 2010 2:09 AM
Hi,
Please use tins for finding the url
Request.Url
OR
HttpContext.Current.Request.Url
Wednesday, December 1, 2010 2:14 AM
Suppose my website address is like http://localhost/MyWebsite
I wanted to access this url in Application_start event in global.asax, Can i some how get it?
[I know i can get it via application_beginrequest or other event]
Your require problem already discuss in Asp.net forum. please visit below thread. You will be get your answer.
http://forums.asp.net/p/1555622/3824567.aspx
Thanks
Wednesday, December 1, 2010 2:50 AM
I have explicitily mentioned about to get it in Application_start event.[Httpcontext is null in that event].I very well know how to get it in Application_BeginRequest but my need is to get the base url in the application_start event
Wednesday, December 1, 2010 2:59 AM
why dont you try this ?
HttpContext.Current.Request.Url
It works for me in Application_start event.
Wednesday, December 1, 2010 3:05 AM
kamii47 you can use this:
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
var context = new HttpContextWrapper(HttpContext.Current);
var routeData = RouteTable.Routes.GetRouteData(context) ?? new RouteData();
var requestContext = new RequestContext(context, routeData);
var urlHelper = new UrlHelper(requestContext);
var url = urlHelper.Action("Home", "Index");
// TODO: do something with the url
}
Wednesday, December 1, 2010 3:18 AM
I am not using Asp.net MVC.I think above mentioned solution is only for asp.net MVC or isn't it?
@Huske
your mentioned solution give me string /Mywebsite instead of http://localhost.Mywebsite or http://localhost:645/Mywebsite [suppose it is a deployed there]
Wednesday, December 1, 2010 3:53 AM
Friday, December 3, 2010 3:08 PM
This works. Please try this in global.asax
void Application_Start(object sender, EventArgs e)
{
StringBuilder sb2 = new StringBuilder();
String pathstring1 = Context.Request.Url.AbsoluteUri;
sb2.Append("Current Executing File Path = " + pathstring1.ToString() + "<br>");
sb2.Append("Is Absolute = " + VirtualPathUtility.IsAbsolute(pathstring1).ToString() + "<br>");
string x2 = sb2.ToString();
}
Monday, December 6, 2010 1:52 AM
I am getting exception
Request is not available in this context.
Monday, December 6, 2010 2:16 AM
I am getting exception
Request is not available in this context.
After I tried, Context.Request is available in Application_Start() method of Global.asax.
Can we read your code so that we can try to find the difference between yours and ours?
Monday, December 6, 2010 2:25 AM
protected void Application_Start(object sender, EventArgs e)
{
........... //some background thread get staqrted
CheckForumsAnswer();//checking answer http://forums.asp.net/t/1628638.aspx
}
private void CheckForumsAnswer()
{
try
{
if (Context != null)
{
StringBuilder sb2 = new StringBuilder();
String pathstring1 = Context.Request.Url.AbsoluteUri;
sb2.Append("Current Executing File Path = " + pathstring1.ToString() + "<br>");
sb2.Append("Is Absolute = " + VirtualPathUtility.IsAbsolute(pathstring1).ToString() + "<br>");
string x2 = sb2.ToString();
LoggingHandler.Log("CheckForumsAnswer string is " +x2, EnumLogTypes.Info);
}
else
{
LoggingHandler.Log("CheckForumsAnswer Contenxt is null ", EnumLogTypes.Info);
}
}
catch (Exception ex)
{
LoggingHandler.Log("CheckForumsAnswer Exception", EnumLogTypes.Info);/// I am getting here when application start event fired
LoggingHandler.Log(ex);
}
}
My Exception Log is as follows
12/06/2010 11:37:37:0760 CheckForumsAnswer Exception
12/06/2010 11:37:37:0810
//******************************************************************************************\\
Exception Occurs At = 12/06/2010 11:37:37:0780
Exception Type = System.Web.HttpException
Exception Message = Request is not available in this context
Exception Source = System.Web
Exception Stack Trace = at System.Web.HttpContext.get_Request()
at PresentationLayer.Global.CheckForumsAnswer()
\\******************************************************************************************//
Monday, December 6, 2010 2:45 AM
Hi Kamran,
Are you using IIS7 Integrated mode?
If so, I am sorry that this is the one of the common errors. Please see Request is not available in this context exception in Application_Start for more information.
Hope this helps.
Monday, December 6, 2010 4:05 AM
Thanks WenChao,
I am using IIS 7.My application will be deployed on IIS5.1, IIS 6, IIS 7 or IIS 7.5 .User are open to use any configuration [integrated mode or not]
From the above link I think i may not able to get the Application URL in the application_start event for all the IIS server in a unique way and might have to rely on applicationj_beginrequest event?
Am I correct in this conclusion?
Wednesday, December 8, 2010 1:16 AM
Thanks Wenchao,
Till to date i have done exactly same implementation as you mentioned above.I were trying to avoid this check if (ApplicationURL == null) on every Application_begin request. I think from the conclusion it is the only option left.