I need to access the website url in Application_Start event. Since HttpRequest in context will not be valid, I am unable to retrieve URL. Are there any alternative to retrieve URL of the application?
For example, I need to fetch https://somehost/root/, assuming the app is hosted under root in IIS.
Thanks In Advance
Try Application_AcquireRequestState:
void Application_AcquireRequestState(object sender, EventArgs e)
{
string url = Request.Url.ToString();
}
Related
I want to make my ASP.NET MVC 3 web app to run on particular hostname or IP address only. If someone tries to host the site on different host or IP address, the website should stop working as it see the hostname/IP address different than configured (basically, hardcoded in the app DLL).
Any idea how effectively this can be achieved in ASP.NET MVC?
In your Global.asax file create new BeginRequest function:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
if (application.Request.Url.Host != "mydomain.com")
{
application.CompleteRequest();
}
}
Filter requests using HTTP Module.
Another way to accomplish it
is creating an action attribute to validate that request :P
public class HostValidatorAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
//validate here and returns true if valid
}
}
I am trying to restrict direct access and downloading of files from my resources folder. I have implemented this in my global.asax:
void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpRequest request = application.Context.Request;
if (request.Url.ToString().Contains(#"/resources/"))
{
Server.ClearError();
Response.Clear();
Response.Redirect(#"http://mysitename.com/download_restriction.aspx");
}
}
It works however, it restricts my pages from using the resources as well... Can I somehow check if the request is being done from one of my pages?
use session variable to know that you have come from your application
I have created an HttpModule so that Whenever I type "localhost/blabla.html" in the browser, it will redirect me to www.google.com (this is just an example, it's really to redirect requests coming from mobile phones)
My Questions are :
1) How do I tell IIS(7.0) to redirect each request to the "HttpModule" so that it is independent of the website. I can change the web.config but that's it.
2) Do I need to add the .dll to the GAC? If so, How can I do that?
3) The HttpModule code uses 'log4net' . do I need to add 'log4net' to the GAC as well?
Thanks
P.S. the site is using .net 2.0.
You can use request object in BeginRequest event
public class MyHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(this.context_BeginRequest);
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//check here context.Request for using request object
if(context.Request.FilePath.Contains("blahblah.html"))
{
context.Response.Redirect("http://www.google.com");
}
}
}
I'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.
Thanks
I originally thought old school, using the ServerVariables collection, as the following worked nicely in my test app, but running under the built in VS web server:
void Application_Start(object sender, EventArgs e) {
// Code that runs on application startup
string localAddress = Context.Request.ServerVariables["LOCAL_ADDR"];
// Returns "::1" when running under the VS server, however it throws an
// exception under IIS Express, so I assume it also does so under IIS.
}
The next best option I can come up with would be something like:
void Application_Start(object sender, EventArgs e) {
// Code that runs on application startup
string serverName = Server.MachineName;
}
Which works on both VS and IIS express, so if you know the name of your test or live servers, you could check against that instead?
Try this.
var ips = Dns.GetHostAddresses(Dns.GetHostName());
if (ips.Any())
{
var serverIp = ips[ips.Count()-1];
}
Dns.GetHostAddresses will return different length due to local or remote server,
that's why i use ips.Count()-1 to get the least one.
Try this:
HttpRequest httpRequest = HttpContext.Current.Request;
string host = httpRequest.Headers["host"];
IPAddress hostAddresse = Dns.GetHostAddresses("host").Where(a=>a.AddressFamily ==AddressFamily.InterNetwork).FirstOrDefault();
Currently, I'm just using clientside Javascript (location.href), but I am wondering if there is a way in Asp.Net to figure out the URL the user originally entered (assume I did not change it myself via 301), or at least to track it in a simple and reliable manner. As I am using my own implementation of URL rewriting via the global.asax (e.g. Context.RewritePath), this is not an easy task, particularly since I don't want to touch it too much.
Example
Global.asax:
public override void Init()
{
base.Init();
this.BeginRequest += new EventHandler(Global_BeginRequest);
}
void Global_BeginRequest(object sender, EventArgs e)
{
if (VARIOUSCONDITIONS) Context.RewritePath("SOMEURL");
}
SomePage.aspx.cs
protected void Page_Init(object sender, EventArgs e)
{
//Request.RawUrl is equal to "SOMEURL", as
//are other properties that store the URL.
}
Maybe I am misunderstanding your question, but if you are trying to capture the page the user first hits on your website, cant you capture this in the session_start event of global.asax? Then store in sessionstate or database for future use?