dottext学习笔记3 URL重写

1 配置

1.1 IIS配置

指定*.*跟*.aspx等一样,其映射到 aspnet_isapi.dll ISAPI 扩展。
这样对性能有很大的影响,因为img,目录等资源都要由aspnet_isapi.dll来解析。
如果不这样的话,指定不存在文件就会出现404错误,网上有定义404错误时重导到ASPX页面,再根据path类型返           
string errorString = context.Request.RawUrl.ToLower();
errorString  == /xxx/404.aspx?404;http://localhost/rewritetest/123456
红体就是原始请求

1.2 Web.config配置

1.2.1 <system.web>节中定义URL重转实现类

1.httpHandlers指定了Error.aspx和*.asmxl类型页面的后台类为系统类,而为指定的所有类型后台类为UrlReWriteHandlerFactory
2.httpModules分别指定了EventHttpModule和UrlReWriteModule作为所有页面的URL请求时的处理。

1.2.2 具体URL重转的实现类配置

1. 在configSections指定解释HandlerConfiguration节的配置解析类为XmlSerializerSectionHandler
2. 该节捆绑的类为HandlerConfiguration,相当类厂
3. <HttpHandlers>节包含很多HttpHandler, HttpHandler后台捆绑页面或类(增加属性handlerType进行区分),若是类均派生自System.Web.IHttpHandler或者IHttpHandlerFactory。


2 类分析

URL处理调用流顺序依次流过n个HttpMoudules,才到Handlers

2.1 HttpModules后台类

均从System.Web.IHttpModule派生
Dottext.Common.UrlManager.UrlReWriteModule
捕捉BeginRequest
public void Init(HttpApplication context)
{
 context.BeginRequest +=new EventHandler(context_BeginRequest);
}
context_BeginRequest:

else   if (context.Request.Path.ToLower().IndexOf( " services " >   0   &&  context.Request.Path.ToLower().IndexOf( " .asmx " >   0  )
                
{
                    
if(AlllowService(context))
                    
{
                        
if(context.Request.RequestType!="POST")
                        
{
                            
string regexstr=@"/w+/services/";
                            
string url=Regex.Replace(context.Request.RawUrl,regexstr,"/services/",RegexOptions.IgnoreCase);
                            context.RewritePath(url);
                        }

                        
//string fileName =context.Request; //System.IO.Path.GetFileName(context.Request.Path);
                        
//context.RewritePath("~/Services/" + fileName);
                    }

                    
else
                    
{
                        context.Response.Clear();
                        context.Response.End();
                    }

                }



另外还有一个HttpModule:
Dottext.Framework.ScheduledEvents.EventHttpModule
处理定时器,定时调用EventManager.Execute();

2.2 HttpHandlers后台类

UrlReWriteHandlerFactory,派生自IHttpHandlerFactory,GetHandler URL重转类厂

实现分析:

a. 截取URL的具体页面名
path = Regex.Replace(Request.Path,"^"+Request.ApplicationPath,string.Empty,RegexOptions.IgnoreCase);
b. 把path跟所有类厂所管理的处理器进行匹配,对匹配的处理根据其类型进行不同的处理
Page:默认。根据具体实现类增加配置中的控件名列表到context中,待后面会用。然后PageParser.GetCompiledPageInstance(url,pagepath,context);来生成页面。
Direct:同上设置控件名列表,同时return (IHttpHandler)items[i].Instance();
Factory:转交factory生成,return ((IHttpHandlerFactory)items[i].Instance()).GetHandler(context,requestType,url,path);
 

3 页面跳转方法

1. Response.Redirect,在客户端跳转,可以使用Session对象传递,不能使用Context对象传递。

2. Server.Transfer,在服务器端跳转,可以使用Session对象传递,也可以使用Context传递。
注意的是,Context对象在页面跳转后即消失,Session对象在Session的有效期内始终存在. ViewState是在本页面保存,因为该方式是将要保存的对象序列化后放在客户端页面中保存,所以保存的对象必须是可序列化的.

3. Server.Execute,在当前位置放置另外一个aspx页面,可合并两个源文件的页面

4. HttpContext.RewritePath 方法,分配一个内部重写路径。
RewritePath 允许请求的 URL 与资源的内部路径有所不同。RewritePath 用于无 cookie 的会话状态中。

5. JavaScritp和VBScrio

A. document.URL属性,如document.URL="edit1.htm";
B. window.location.href
<input type="button" name="jump" value="跳转" οnclick=window.location.href="page2.asp";>
function logout(){ 
if (confirm("你确定要注销身份吗?/n是-选择确定,否-选择取消")){ 
window.location.href="logout.asp?act=logout" 

C. window.open
window.open("page2.asp?ID="+id+"&act=del","top","width=640,height=400")
function logout(){ 
if (confirm("你确定要注销身份吗?/n是-选择确定,否-选择取消")){ 
window.location.href="logout.asp?act=logout" 

D. Response.Redirect
<%@ Language="VBScript" %>
<%
Response.Redirect(Request.ServerVariables("URL") & "x?" & & Request.QueryString)
//Response.Transfer(Request.ServerVariables("URL") & "x" ) 会出错,因为asp解析器不能解析aspx的文件
%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值