What if URL rewriting does not support relative paths?

Source: Internet
Author: User

URL rewriting is common, but basically most URL rewriting does not support relative paths on pages. If you want to add a URL to a developed project, it is still stressful, the second is that, for example, Microsoft's URL rewriting is handled based on regular expressions, which is very good, but there are also deficiencies, that is, it is not convenient to locate what parameters a page can only have.

I think there are several problems to solve:

1. Solve file files such as slice JS that cannot use relative paths

2. Fixed several parameters and optional parameters for a page.

The following is how to solve these problems.

Add the handler myhttpmodule. Below is a simple handler (I just made a simple one without considering the performance, in addition, if I write a dead URL, rewrite it to no extension)

The following is a code snippet:
Using system;
Using system. Collections. Generic;
Using system. Web;
Using system. IO;
Using system. text;

Namespace myclass
{
Public class myhttpmodule: ihttpmodule
{
# Region ihttpmodule Member
/// <Summary>
/// Release all resources
/// </Summary>
Public void dispose ()
{
}
/// <Summary>
/// Initialize the module and prepare it for processing requests
/// </Summary>
/// <Param name = "context"> A system. web. httpapplication, which provides.. NET application, and access to common methods, properties, and events of all application objects </param>
Public void Init (httpapplication context)
{
Context. authorizerequest + = new
Eventhandler (this. basemodulerewriter_authorizerequest );
}
/// <Summary>
/// This occurs when the security module has verified 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>
/// Rewrite the URL
/// </Summary>
/// <Param name = "requestedpath"> virtual path of the URL </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); // obtain the actual file information
If (system. Io. Path. getextension (inputfile) = ". aspx") // if it is An ASPX file, the reserved URL is overwritten.
{
App. context. rewritepath (requestedpath, String. empty, String. empty); // I have not processed the query parameters here, that is, the request. querystring information. If qerystring is taken and then processed as a string
Return;
}
App. Context. rewritepath (virtualpath, String. Empty, app. Context. Request. querystring. tostring (); // use the path found for other files.
}
/// <Summary>
/// Obtain the absolute path and virtual path corresponding to the URL and query parameters
/// </Summary>
/// <Param name = "context"> </param>
/// <Param name = "virtualpath"> virtual path </param>
/// <Param name = "qerystring"> If the query parameter is null, use httpcontext. Request. querystring. </param>
/// <Returns> absolute URL </returns>
Public static string getinputfile (httpcontext context, out string virtualpath, out list <string> qerystring)
{
String executionfilepath = context. Request. apprelativecurrentexecutionfilepath. Remove (0, 2); // obtain the current virtual path and kill "~ /"
String inputfile = context. Request. physicalpath; // obtain the absolute path of the current URL
Virtualpath = context. Request. apprelativecurrentexecutionfilepath;
Qerystring = NULL;
List <string> qerylist = new list <string> ();
If (! File. exists (inputfile) // determines whether the file exists, that is, the URL that has not been overwritten is used to obtain resources using the absolute path, etc.
{
Bool B = false;
String filename;
String extension;
String applicationpath = context. Request. physicalapplicationpath; // obtain the website's directory
VaR temppath = getfileinfo (inputfile, out filename, out extension );
While (! B) // obtain a valid file directory cyclically Based on the directory
{
B = file. exists (temppath + "\" + extension); // checks whether the file exists.
If (temppath + "\" = applicationpath) // if the root directory is not found, you do not need to query
{
Break;
}
If (! String. isnullorwhitespace (filename ))
{
Qerylist. Add (filename); // if it does not exist, this is the parameter such as http: // localhost: 4688/webform1/2011/(corresponding to http: // localhost: 4688/webform1.aspx? XXXXX = 2011)
}
Temppath = getfileinfo (temppath, out filename, out extension );
}
If (B) // If yes, copy the path to the output or return parameter.
{
Inputfile = temppath + "\" + extension;
Virtualpath = "~ /"+ Inputfile. Replace (applicationpath, null );

}
If (path. getextension (Extension) = ". aspx") // if it is Asp.net, copy the list to the output parameter qerystring.
{
Qerystring = qerylist;
}
}
Return inputfile;
}
/// <Summary>
/// Obtain 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; // obtain the parent directory of the imported directory.
Filename = inputfile. Replace (temppath + "\", null); // obtain the subdirectory name
Extension = path. getextension (inputfile); // get the extension
If (string. isnullorwhitespace (Extension) // if the extension is null, it is considered as An ASPX file.
{
Extension = filename + ". aspx ";
}
Else
{
Extension = filename + extension;
}
Return temppath;
}
# Endregion
}
}

Related message:

Http://www.sexyunique.com/article-94.html
Http://www.sexyunique.com/sitemap.html
Http://wangzhanshiyanshi.blogbus.com/logs/190610175.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.