How To Get The IP Address Of The Current Website In Global.asax
Nov 16, 2010I'd like to do this in the Application_Start method, so I can write a 'robots.txt' file if the site is running on a test server.
View 2 RepliesI'd like to do this in the Application_Start method, so I can write a 'robots.txt' file if the site is running on a test server.
View 2 RepliesI'm using VS2010 and created a simple asp. web forms application, using Development Server to test it. I try to store user data - queried from sql server - in the session, since I don't want to access database in every request. I'm using the 'Application_AuthenticateRequest' and the 'Session_Start' methods.
First round:
AuthenticateRequest called. The following code ran:
[Code]....
The _current variable is a private static field published through a static property. In the first round the Session is null, and I think it's ok because the Session_Start not called yet. The Session_Start looks like this:
protected void Session_Start(object sender, EventArgs e)
{
Session["EMPLOYEE_DATA"] = EmployeeFactory.Current;
}
In the next round the Session_Start is not called of course but in the AuthenticateRequest I can't access to the session. The HttpContext.Current.Session is null and the this.Session reference throw a HttpException says the "Session state is not available in this context".
However I can access the Session from any of the page_load events but it's a bad practice I think that I put authentication every page_load.
I have a website with a global.asax file with some debug code in Application_Start, which inserts some debug information in a table in the db:
void Application_Start(object sender, EventArgs e)
{
Log.LogToDb("App Starts");
}
I have 2 versions of the same website, one compiled and the other one just plain code.The plain code website correctly inserts these records in the db, but the compiled one is not doing it.
I have a feeling the Global.asax file is being ignored by the compiled website. I can see the dll for it in the in folder: App_global.asax.compiled and App_global.asax.dll although I can't see a Global.asax file in the root folder (do I need one?)
I must say the website is compiled with MSBuild and then obfuscated using Dotfuscator.
I have recently published my website, it worked fine but the only issue is that the error handling code in the global.asax never works. It works when I tested the website on my localhost but not when it is deployed onto my hosting provider.
I tried moving my global.asax file onto my root directory (not the bin) on the hosting folder, then my whole website wouldn't even start. I get the error message "System.Web.HttpException: This server variable cannot be modified during request execution."
But if I remove the global.asax, then it is running fine again.
Note: I didn't use the publish function in Visual Studio to publish my website, I sent the files over one by one via cPanel. Note: I googled about this, a lot of them are saying about global.asax being precompiled into a dll, but I can't find that dll in my local computer.
how can I implement a Global Session in my ASP.NET 3.5 website in C# using Global.asax file ?
View 18 RepliesWhen I run my app locally I get no errors. No error from Global.asax, no redirection to an error page. Everythings works fine.
When I deploy my app to a test server, my app run there with no errors. No error from Global.asax, no redirection to an error page. Everythings works fine on the test server.
But when I deploy my app to production server, the app runs fine but I keep getting errors (emailed to me) from Global.asax. On production server we have these security apps that run and keep hitting all URLs of the app on the server. Just to make sure they are working fine. When these security app hit URL of my app, my application runs fine but it gives out this global.asax error. The funny thing is that error is empty. The error are emailed to me and the email is empty. Nothing there.
There are several other apps running on the same server. My app has its own app pool and is configured correctly.
I know there is a couple answered questions on here regarding "request scoped" globals, but I want to nit-pick on something specifically and maybe squeeze some extra enlightenment out of one or two of you.I have an ASP.NET C# Website and a static Dictionary of objects (loaded from DB once on Application start). Each page request will need to do a lookup in the Dictionary (based on a key derived from the request url/etc) and get the appropriate object.The issue is I'm trying to maximize efficiency by reducing the lookups to the Dictionary per Request. Doing just a single lookup within a Page itself is easy enough and I can pass the object to sub controls, etc too.. but global.asax is separate from the Page and it also needs to use the object (in Application_BeginRequest and Session_Start).
So is doing a Dictionary lookup once in Application_BeginRequest, once (when necessary) in Session_Start and once in the Page negligible speed wise, even if there are many requests coming in every second?I would like it if I could just have a Request scoped global variable that I can easily call upon.. the only one I see available though is HttpContext.Current.Items and that is a Dictionary itself.Am I beingridiculously nit-picky with my concern over efficiency? or will these milliseconds (nanoseconds?) get me in the long run when more and more requests are being made?
PS. I currently only have around 100 objects in the Dictionary although this may increase in the future.
I am trying to pull entries from a database based on the current users IP Address. Here is what I have so far:
[Code]....
I am receiving the following error: Cannot find either column "Request" or the user-defined function or aggregate "Request.UserHostAddress.ToString", or the name is ambiguous.
Is it possible to do this? Should I try to access this information another way?
how can we use global.asax in asp.net? and what is that?
View 4 RepliesIs it possible in the global.asax file on session_start to check if a url string exists and it it does insert the string into a sql database? The url would be something link http://www.mysite.com?campaign=january.
I would want to capture the january bit and put that in the database for each visitor that uses that url.
I want to make All Methods about Error handling in asp.net some one device me at using global.asax what is global.asax?
how can i use it ?
I have a hosting account on DailyRazor.com , i use Global.asax in my project to start a schedule process in Application_Start.When i just publish my website, and upload it, the Application_Start does not Fire, but when i upload the Global.asax to my host it gives an Error but the Applicaiton_Start Event fires up, and then i delete the Global.asax file and everything works.just wondering, What am doing wrong ? or why this meaningless thing happens?
View 2 RepliesI have a site with multiple domains pointing to it. I wanted to redirect all requests to main domain so I've created a method called RedirectToRealDomain("domain.com") to check and redirect all requests to my preferred domainAt the moment it lives on Session_Start but I am planing to move it to Application_BeginRequest event. As I understand Session_Start only raised when new session started but Application_BeginRequest raised on all requests. RedirectToRealDomain method doesn't do any DB lookups or anything expensive apart from comparing strings.
View 2 RepliesI want to set the Masterpage property in Global.asax.
This is what I have done but I get a NullReferenceException on the first line.
protected void Application_PreSendRequestContent(Object sender, EventArgs e)
{
System.Web.UI.Page page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;
if (Session["lang"] == "eng")
{
page.MasterPageFile = "SideMasterPageEng.master";
}
}
I am trying to check if the user belongs to someone's friendlist from the database and redirect him accordingly.I am doing this in a routehandler called by Global Asax.I just want to know how to get the username (from the login information) in the route handler class (or Global asax)I used this: string username = HttpContext.Current.User.Identity.Name; and very strangely, its assigning ".aspx" as the username!! ps: i did search for similar question but in vain.
View 1 RepliesI need replace double slashes in one slash. I am planning do this in Global.asax Application_BeginRequest event. Is it enough? or better do a http module?
UPD
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
[code]...
I was under the impression till now that Global.asax is used for application level events and it's a common file for all users. Then what does Session_Start event does in this file. If we have to create user session varaibles then Is this the write place to create those? If someone can kindly clear my confusion on this file and it's methods.
View 5 RepliesI'm trying to handle all my application exceptions inside the global.asax but from some reason it's not firing at all, i never tried to do it before so i guess i'm doing something wrong here...
here is the code inside the default.aspx page that throw the error:
[Code]....
I'm using the HttpRunTime.Cache.Add to create a routine that runs every 5 mins to post data out of my app to the main DB, and this works fine. However I also need to import some files from disk in this routine. I have my document directory set up in my web.config as a ~/ relative path and resolve this in normal controller actions using Server.MapPath(string). How can I do this in my Global.asax.cs code -when I try to access it, it throws an error?
View 3 RepliesI have a custom error handler in the global.asax's Application_Error method, when an error occurs I use the following code to transfer user to the error page:Server.Transfer("/Error/");
However without specifying the actual page name the code above breaks with "Error executing child request for /Error/" exception.So if I use Server.Transfer("/Error/Default.aspx") it works fine with no problems.Also using Response.Redirect("/Error/") works fine too, however we want to keep using Server.Transfer to retain URL displayed in the address bar when the error page is displayed so users can simply refresh the page to retry accessing the original offending URL.Would be grateful if anyone can comment on how to get the Server.Transfer method working without specifying the actual aspx page name.
I've got an website running on a third-party hosting server. Now I've got a few objects in global asax. But once in like 6 hours or so (didn't really check how long) the object gets reset to default values.Is this an problem in global asax that it reset's himself or is this the server on which I'm hosting?
View 3 Replieswe have a store that we contracted with a company to modify. They added the following to our Global.asax file:
[code]...
The purpose is to take anyone going to one of our pages to a login screen unless they are going to these folders which do not need the login protection.
I now need to let them go to [URL]
The purpose of the code associated with the Global.asax is to hold functionality to respond to various application-level events.
But why is there a markup file associated with the Global.asax.cs? I presume this is an ASP.NET implementation side effect?
I was given a web app written in VB.net. I am using VS2008. The project has a Global.asax page with Enums declared
Example:Public Enum eCurrentLoc as Integer
Admins
AdminTools
Home
LogIn
Status
End Enum
But in the web application itself I get errors on this line of code:
Ads = Global.eCurrentLoc.Admins
I get the blue scwigly line under Global.eCurrentLoc
The error states 'eCurrentLoc' is not a memeber of '<Defaults>'
I need to redirect some of the older pages in my application to new pages. I thought urlMapping in web.config is the efficient way to achieve this. But there is also another way to redirect using global.asax. Which one is the efficient way for this. At what point in request execution does this asax and config file comes into the picture?
View 3 Replies