zoukankan      html  css  js  c++  java
  • 在Asp.Net里使用自定义映射进行重定向

      在IIS站点属性中添加自定义映射,如添加一个扩展名为.fbsx的文件类型,通过实现IHttpHandler来进行重定向。

      实现IHttpHandler的类:
     1using System;
     2using System.Text.RegularExpressions;
     3using System.Web;
     4using System.Web.SessionState;
     5
     6namespace FaibClass.Common.Web
     7{
     8    public class URLRewriterHandler : IHttpHandler, IRequiresSessionState
     9    {
    10        public void ProcessRequest(HttpContext context)
    11        {
    12            URLRewriterRuleCollection rules = URLRewriterConfiguration.GetConfig().Rules;
    13            string requestedPath = context.Request.FilePath;
    14            for(int i = 0; i < rules.Count; i++)
    15            {
    16                Regex reg = new Regex(rules[i].MatchUrl, RegexOptions.IgnoreCase);
    17                bool isMatch = reg.IsMatch(requestedPath);
    18                if(isMatch)
    19                {
    20                    context.Server.Execute(reg.Replace(requestedPath, rules[i].RedirectUrl));
    21                }

    22            }

    23        }

    24
    25        public bool IsReusable
    26        {
    27            get
    28            {
    29                return true;
    30            }

    31        }

    32    }

    33}

      使用IRequiresSessionState的目的是:能够在目标页面中使用Session。注意如果目标页面中使用Ajax.Net组件,在注册类型的时候应使用第二个参数:
    AjaxPro.Utility.RegisterTypeForAjax(typeof(YourClass), this);

      打开IIS管理器,在站点属性的映射中添加一个.fbsx的文件扩展名,其他设置与.aspx的一致,注意“确认文件是否存在”不能勾选。

      在web.config中配置httpHandlers节:
    1 <httpHandlers>
    2   <add verb="POST,GET" path="*.fbsx" type="FaibClass.Common.Web.URLRewriterHandler, FaibClass.Common" />
    3 </httpHandlers>
      并配置重定向配置节
     <configSections>
       
    <section name="URLRewriterConfig" type="FaibClass.Common.Web.URLRewriterConfigSerializerSectionHandler, FaibClass.Common" />
      
    </configSections>
      
    <URLRewriterConfig>
        
    <Rules>
           
    <URLRewriterRule>
             
    <MatchUrl>([\d]+)\.fbsx</MatchUrl>
             
    <RedirectUrl><![CDATA[target.aspx?to=$1]]></RedirectUrl>
           
    </URLRewriterRule>
         
    </Rules>
      
    </URLRewriterConfig>

      这样,只要我们键入一个1234.fbsx文件,浏览器就会重定向到target.aspx页面进行相应的执行。
  • 相关阅读:
    155. 最小栈
    160. 相交链表
    PAT 1057 Stack
    PAT 1026 Table Tennis
    PAT 1017 Queueing at Bank
    PAT 1014 Waiting in Line
    PAT 1029 Median
    PAT 1016 Phone Bills
    PAT 1010 Radix
    PAT 1122 Hamiltonian Cycle
  • 原文地址:https://www.cnblogs.com/faib/p/717776.html
Copyright © 2011-2022 走看看