|
Using System;
Using System.Collections.Generic;
Using System.Web;
Using System.IO;
Using System.Text;
Namespace MyClass
{
public class Myhttpmodule:ihttpmodule
{
#region IHttpModule Members
<summary>
Free All Resources
</summary>
public void Dispose ()
{
}
<summary>
Initialize the module and make it ready for processing requests
</summary>
<param name= "Context" > a System.Web.HttpApplication that provides access to common methods, properties, and events for all application objects within the ASP.net application </param >
public void Init (HttpApplication context)
{
Context. AuthorizeRequest + = new
EventHandler (this. Basemodulerewriter_authorizerequest);
}
<summary>
Occurs when a security module has authenticated a user authorization
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected virtual void Basemodulerewriter_authorizerequest (
Object sender, EventArgs e)
{
System.Web.HttpApplication app = (System.Web.HttpApplication) sender;
Rewrite (app. Request.path, app);
}
<summary>
overriding URLs
</summary>
<param name= "Requestedpath" >url virtual path </param>
<param name= "App" ></param>
protected void Rewrite (String requestedpath, System.Web.HttpApplication app)
{
List<string> qerystring;
String virtualpath;
String inputfile = Getinputfile (app. Context, out virtualpath, out qerystring);//Get real file information
if (System.IO.Path.GetExtension (inputfile) = = ". aspx")//If it is an ASPX file, then leave the overridden URL
{
App. Context.rewritepath (Requestedpath, String. Empty, String. Empty)//Here query parameters I did not deal with, that is, request.querystring information, if you take qerystring and then to process into a string
Return
}
App. Context.rewritepath (VirtualPath, String. Empty, App. Context.Request.QueryString.ToString ());//Other files use the found path
}
<summary>
Get the absolute path and virtual path and query parameters corresponding to the URL
</summary>
<param name= "Context" ></param>
<param name= "virtualpath" > virtual path </param>
<param name= "qerystring" > query parameters if NULL please fetch httpcontext.request.querystring</param>
<returns>url corresponding Absolute path </returns>
public static string Getinputfile (HttpContext context, out string virtualpath, out list<string> qerystring)
{
String Executionfilepath = context. Request.AppRelativeCurrentExecutionFilePath.Remove (0, 2);//Get the current corresponding virtual path and kill "~/"
String inputfile = context. request.physicalpath;//gets the absolute path of the current URL for
VirtualPath = context. Request.apprelativecurrentexecutionfilepath;
qerystring = null;
list<string> qerylist = new list<string> ();
if (! File.exists (Inputfile))//To determine whether a file exists, that is, a URL that is not overridden to obtain a resource using an absolute path, and so on
{
BOOL B = false;
String FileName;
string extension;
String Applicationpath = context. request.physicalapplicationpath;//access to the directory of the website
var TempPath = GetFileInfo (inputfile, out fileName, out extension);
while (!B)//Gets a valid file directory based on the directory loop
{
b = file.exists (TempPath + "\" + extension);//Determine if the file exists
if (TempPath + "\" = = Applicationpath)//If you find the root directory and haven't found it yet, then you don't need to check it out.
{
Break
}
if (!string. Isnullorwhitespace (FileName))
{
Qerylist.add (fileName);//If not present then this is the parameter such as http://localhost:4688/WebForm1/2011/(corresponds to http://localhost:4688/WebForm1.aspx?) xxx=2011)
}
TempPath = GetFileInfo (TempPath, out fileName, out extension);
}
if (b)//If found, copy the lookup path to the output or return parameter
{
Inputfile = TempPath + "\" + extension;
virtualpath = "~/" + inputfile.replace (Applicationpath, NULL);
}
if (path.getextension (extension) = ". aspx")//if ASP.net then the list is copied to the output parameter qerystring
{
qerystring = qerylist;
}
}
return inputfile;
}
<summary>
Gets whether the specified directory + file is valid
</summary>
<param name= "Inputfile" > Directory </param>
<param name= "FileName" ></param>
<param name= "extension" ></param>
<returns></returns>
private static string GetFileInfo (String inputfile, out string fileName, out string extension)
{
var TempPath = directory.getparent (inputfile). fullname;//gets the parent directory of the passed in directory
FileName = Inputfile.replace (TempPath + "\", null);//Get subdirectory name
Extension = Path.getextension (inputfile);//Get extension
if (string. Isnullorwhitespace (extension))//If the extension is null then the ASPX file is considered
{
Extension = FileName + ". aspx";
}
Else
{
Extension = fileName + extension;
}
return temppath;
}
#endregion
}
} |