I'm new to development with .NET and working on a personal project. My project will allow users to create their own simple mobile site.
I would like to write a HTTP Module that will handle pseudo sub-domains.
I have already setup my DNS wildcard, so sub.domain.com and xxx.domain.com etc. point to the same application. I want to be able to extract sub and ID parts from sub.domain.com/pageID.html URL and load settings of the page from a database server in order to build and render the page to the client.
I can do it with URL rewrite tools like isapirewrite, but I want my application to be independent from OS so that the server doesn't require installation of any 3rd party app.
Is it possible to do it with HTTP handlers?
Can anyone post an example?
You can check the domain at any time. Where to do it dependings on your application's goals. E.g. if you want to serve different pages depending on the domain you could do like this:
public class MyModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += context_BeginRequest;
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
string host = app.Request.Url.Host;
if(host == "first.domain.com")
{
app.Context.RewritePath("~/First.aspx");
}
}
}
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 have a website with files which are static like Jquery library, images and other JS files.
So, I wish to set expiry time for those resources specifically so that those can be easily retrieved from users cache and without caching other static resources
can anybody suggest a way to do that in asp.net 3.5?
Thank You
You should separate this static files in folder and configure it directly on IIS
Here's a example for IIS6:
http://www.websiteoptimization.com/secrets/advanced/9-7-content-expiration-IIS.html
Or via code you can implement an IHttpModule
public class CacheExpiresModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
string url = context.Request.Url.ToString();
if (url.Contains("/Static/"))
{
context.Response.Cache.SetExpires(DateTime.Now.AddYears(30));
context.Response.Cache.SetMaxAge(TimeSpan.FromDays(365));
}
}
}
and configure it on your web.config
You can leverage browser caching setting an expiration data through http headers. There is a brief explanation of this process from Google Developers / Page Speed Insight:
https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
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 have written a static class and suppose to share it's members between all sessions. it works for all the browsers running on the same cumputer but the data is not shared between different users from different location. my website is written with ASP.NET
this is my class
public static class GlobalPool
{
public static List<string> OnlineUsers;
}
and I instantiate the OnlineUSers property in Global.asax as
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
GlobalPool.OnlineUsers = new List<string>(100);
}
I add username whenever a user logs into my website:
public void Login(int aMemberSerial)
{
User = new MemberDataAccess().Read(aMemberSerial);
new MemberDataAccess().Login(User);
GlobalPool.OnlineUsers.Add(User.Username);
Message = PostBusiness.NewPost(User);
}
This is not robust code. ASP.NET application routinely recycle (the App Pool) and so all your data will go away.
The best way to approach this is to store the list in a database, and when a Session expires, remove that user from the data store.
I have this code
//file Globals.cs in App_Code folder
public class Globals
{
public static string labelText = "";
}
and a simple aspx page which has textbox, label and button. The CodeFile is:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Globals.labelText;
}
protected void Button1_Click1(object sender, EventArgs e)
{
Globals.labelText = TextBox1.Text;
}
}
That is when I click on the button the Globals.labelText variable initializes from the textbox; the question is: why when I open this page in another browser the label has that value, which I set by the first browser, that is the static member is common for the every users. I thought that the every request provides in the individual appDomain which created by the individual copy of IIS process. WTF?
Yes you may use static variable to store application-wide data but it is not thread-safe. Use Application object with lock and unlock method instead of static variables.
Take a look at ASP.NET Application Life Cycle Overview for IIS 7.0 and ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0
No, static in this case is static in that manner only for the lifecycle of the process the request lives on. So this variable will be static the entire time you're processing a single request. In order to have a "static" variable in the manner you describe, you'd have to make it an application variable. Something like this:
//file Globals.cs in App_Code folder
public class Globals
{
// I really recommend using a more descriptive name
public static string LabelText
{
get
{
return Application("LabelText") ?? string.Empty;
}
set
{
Application("LabelText") = value;
}
}
}
By making it an application variable it should survive multiple page requests. A vulnerability it has though is that it will not survive an application pool recycle, and for large applications this can be problematic. If you truly want this variable to behave in a static manner reliably you're probably better off storing its state in a database somewhere.