url静态映射及回发处理

URL重写是截取传入Web 请求并自动将请求重定向到其他 URL 的过程。

比如:浏览器发来请求 http://localhost:90/URLRewriter/1.html ,

服务器自动将这个请求中定向为http://localhost:90/URLRewriter/url.aspx?id=1

URLRewriter下载编译后提取其中的URLRewriter.dll和ActionlessForm.dll

一、URL重写

项目引用URLRewriter.dll

web.config配置:

代码
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>

<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/URLRewriter/(.[0-9]*)\.html</LookFor>
<SendTo>~/URLRewriter/url.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/web</LookFor>
<SendTo>~/URLRewriter/url.aspx</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>

<system.web>
<httpHandlers>
<!--URLRewriter begin 使用 HTTP 处理程序执行重写-->
<!--<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />-->
<!--URLRewriter end-->
</httpHandlers>

<httpModules>
<!--URLRewriter begin 使用 HTTP 模块执行重写 -->
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
<!--URLRewriter end-->
</httpModules>
</system.web>

</configuration>
IIS配置:

网站--属性--主目录--配置--插入--C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll

将文件是否存在 勾去掉

二、处理PostBack回发

ActionlessForm.dll以重写Form的方式用于处理PostBack回发后URL变为原始地址

项目引用ActionlessForm.dll在页面中注册一下

<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>

将页面中的<form runat="server"></form>替换成:<skm:form id="form1" runat="server"></skm:form>

注:以这种方式处理回发将会在设计器中查看的时候为错误提示Form不可用

所以采用以下方法处理:

在微软的URLRewriter类库中添加以下类之后编译


代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// FormRewriter 的摘要说明
/// </summary>
namespace URLRewriter.Form
{
public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
{
public FormRewriterControlAdapter()
{
}

protected override void Render(HtmlTextWriter writer)
{
base.Render(new RewriteFormHtmlTextWriter(writer));
}
}

public class RewriteFormHtmlTextWriter : HtmlTextWriter
{
public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
: base(writer)
{
base.InnerWriter = writer.InnerWriter;
}
public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
: base(writer)
{
base.InnerWriter = writer;
}

public override void WriteAttribute(string name, string value, bool fEncode)
{
//If the attribute we are writing is the "action" attribute, and we are not on a sub-control,
//then replace the value to write with the raw URL of the request - which ensures that we'll
//preserve the PathInfo value on postback scenarios
if (name == "action")
{
HttpContext context = HttpContext.Current;
if (context.Items["ActionAlreadyWritten"] == null)
{
//We will use the Request.RawUrl property within ASP.NET to retrieve the origional
//URL before it was re-written.
value = context.Request.RawUrl;
//Indicate that we've already rewritten the <form>'s action attribute to prevent
//us from rewriting a sub-control under the <form> control
context.Items["ActionAlreadyWritten"] = true;
}
}
base.WriteAttribute(name, value, fEncode);
}
}

}
在App_Browsers文件夹下创建Form.browser

代码
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="URLRewriter.Form.FormRewriterControlAdapter" />
</controlAdapters>
</browser>
</browsers>
这样就不需要引用ActionlessForm.dll也不需要改变Form了,只要引用URLRewriter.dll就可以了

三、在处理重写成html的时候本来网站中的html页面将会不能使用

使用以上方式将不存在找个问题

如果还不行可以在<compilation debug="true">节点下添加

<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>

在<httpHandlers>节点下添加(如果之前使用的是http处理程序执行重写的,请写在前面)

<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值