How to elegantly handle ReturnUrl when using UrlRewrite in ASP.NET 2.0 WebForms

前端 未结 4 1747
情歌与酒
情歌与酒 2021-01-13 03:12

I have a folder with multiple .aspx pages that I want to restrict access to. I have added web.config to that folder with .

The

4条回答
  •  耶瑟儿~
    2021-01-13 03:42

    Create the following control adapter to rewrite the form tag with the for the action attribute. I used this a ASP.NET 2.0 app in conjunction with the Intelligencia url rewriter. I got it from this blog post from the Gu.

    Put this class in your App_Code folder:

    using System.IO;
    using System.Web;
    using System.Web.UI;
    
    public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(new RewriteFormHtmlTextWriter(writer));
        }
    }
    
    public class RewriteFormHtmlTextWriter : HtmlTextWriter
    {
        public RewriteFormHtmlTextWriter(TextWriter writer) : base(writer)
        {
            base.InnerWriter = writer;
        }
    
        public RewriteFormHtmlTextWriter(HtmlTextWriter writer) : base(writer)
        {
            this.InnerWriter = writer.InnerWriter;
        }
    
        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"))
            {
                if (HttpContext.Current.Items["ActionAlreadyWritten"] == null)
                {
    
                    // Because we are using the UrlRewriting.net HttpModule, we will use the 
                    // Request.RawUrl property within ASP.NET to retrieve the origional URL
                    // before it was re-written.  You'll want to change the line of code below
                    // if you use a different URL rewriting implementation.
                    value = HttpContext.Current.Request.RawUrl;
    
                    // Indicate that we've already rewritten the 
    's action attribute to prevent // us from rewriting a sub-control under the control HttpContext.Current.Items["ActionAlreadyWritten"] = true; } } base.WriteAttribute(name, value, fEncode); } }

    Then, create this .browser file in your App_Browsers folder:

    
      
        
          
        
      
    
    

提交回复
热议问题