惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - Notus|南色的风

selenium cant start a new browser(ie7):java.lang.StringIndexOutOfBoundsException: String index out of range: -1 修改sql server2005的系统时间 conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. java移动/复制文件 copy/move file eclipse输出jar的利器 java调用c#写的webservice java的Date.getTime()转换成C#的Datetime.ticks - Notus|南色的风 - 博客园 我的新博客 关于小凡 关于迷笛音乐节的重大通知-_- 给我一点自由...... 4月份的歌 都说了,一切都会过去 everything Will Flow Take It Away song2 【Ireland On-Line】霍利尔:裁判毁了枪手 【每日邮报】利物浦对阵阿森纳能走出牢狱要谢天谢地谢Peter Turn into
asp.net用url重写URLReWriter实现任意二级域名(续)
Notus|南色的风 · 2009-03-26 · via 博客园 - Notus|南色的风

更高级的内容看这里:
http://www.cnblogs.com/notus/archive/2007/03/13/673222.html
最近有个朋友要做url重写的东西,我帮他弄了弄,回头看当年自己写的那个文章,当时以为自己写的很容易理解.但现在再看却觉得写的不好.而今天百度了一下urlrewriter发现我这个文章竟然排第二.为了方便更多朋友,我再写点东西补充下.
一般用百度搜的朋友都是对这个不了解但又急需要用的,我想,再多的语言也比不过一句代码.于是我把今天帮朋友时候写的一个小例子传了上来.这个小例子的目的是实现对任意url的重写(但不包括二级域名的,有需要二级域名的也可以先了解下url重写的概念). 这个小项目的制作过程如下 

1.需要对诸如http://song2.cn/viewnews/2009/3/2.html的url进行重写.将其重写为http://song2.cn/viewnews.aspx?id=2&year=2009&month=3
2.下载微软的那个URLRewriter.dll,使用这个对url进行重写.

我们来看这个url, http://song2.cn/viewnews/2009/3/2.html(当然也可能是3.html,6.html,7.html).要重写,首先是截获url请求,然后分析当时的url,最后跳转到相应的页面.所以我们第一步是要截获url请求.为此新建一个类库叫URL,在该类库中引用URLRewriter.dll.新建一个类myrewritter.cs,代码如下

namespace URL
{
    
public class myrewritter : URLRewriter.BaseModuleRewriter
    {
        
protected override void Rewrite(string requestedPath, HttpApplication app)
        {
            
if (requestedPath.Contains("viewnews/2009/3/2.html"))
                app.Context.RewritePath(
"/viewnews.aspx?id=2&year=2009&month=3");
            
else
                app.Context.RewritePath(
"/here.aspx");
        }
    }
}

可以看到该类继承了URLRewriter.BaseModuleRewriter,然后在Rewrite方法中加入了我们自己的逻辑.现在,只要当前的url请求是viewnews/2009/3/2.html,我们就会将页面重写到/viewnews.aspx?id=2&year=2009&month=3.
这只是一个简单的例子,实际上对于url的判断,一般是用正则表达式来完成.而页面和页面间的对应关系,可能需要通过查询数据库完成.

接下来在网站项目中引用这个类库.然后修改下web.config,下面是我的web.config

<?xml version="1.0"?><configuration>
    
<appSettings/>
    
<connectionStrings/>
    
<system.web>
        
<httpModules>
            
<add type="URL.myrewritter"  name="URL" />
        
</httpModules>        
        
<compilation debug="true" />     
        
<authentication mode="Windows" />
    
</system.web>
</configuration>

大家可以看到我新加了个httpModules.这个的作用是,如果有url请求,asp.net会先把请求传到你指定的这个类中来.add那一行了,前面的是要接收请求的类,后面的是这个类所在的dll的名字.

添加完这一句后,一切就快结束了.由于我们要处理的是.html的页面,所以需要配置下iis.因为默认asp.net的处理引擎不会管.html.我们要做的是用处理asp.ent的那个程序来处理html页面.打开iis.找到你的网站,点右键打开属性.找到主目录(Home Directory),打开配置(Configuration)对话框. 在Application extensions(抱歉我不知道这个在中文系统里叫什么)列表里找到.aspx,点击编辑(Edit...),复制Executable框的内容.比如我的是:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll. 然后点击Add,在打开的对话框里, Executable把刚才复制的路径粘贴到这里,extension填写html,然后确定保存.这样我们就把对.html页面的请求交给了asp.net.

全部完成了,编译下项目(中文环境叫 生成),打开url http://localhost/mytest/viewnews/2009/3/2.html,看会不会重写到http://localhost//viewnews.aspx?id=2&year=2009&month=3

下面是我的例子的全部文件,大家可以下载然后看看.看一句代码比看十句话都管用,对吧

https://files.cnblogs.com/notus/rewrittertest.rar

要注意的是,我的例子中,web网站的url配置成了http://localhost:8011,你可能需要更改下这个配置.方法是在网站项目上点右键,打开属性.然后在Web页面中修改.

作为一个初学者,可能对这些东西感觉挺烦的.如果你不想看长篇大论,那最好的办法就是看下我的代码.我最近在学flex,对有些新东西也是处于忍无可忍的状态.春天到了,大家都比较烦躁,所以,静下心来,慢慢来.

happy programming :)