• .NET下URL重写及PostBack处理


    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"/>

     

    以下为IIS7.0配置方式

    IIS7不再使用URLRewriter.dll组件了,请使用IIS7的URLWRITER,下载rewrite_2.0_rtw并进行安装,配置
  • 相关阅读:
    git command
    MySQL命令行学习
    添加react-router
    nodejs 安装出错总结
    切换分支之后,意外出现的,待提交的改变
    git diff
    git log
    搜索的技巧
    x-shell code
    css汇总
  • 原文地址:https://www.cnblogs.com/hejunrex/p/1635172.html
Copyright © 2020-2023  润新知