简体   繁体   中英

Changing postback URL to hide Default.aspx

I am attempting to hide the document name from the user by using a folder with appending querystring in the following format:

http://localhost:53779/s/?x=FF2F60195B21487FA19A8EE7767A206C

When I post back the page, it directs it to the physical page:

http://localhost:53779/s/default.aspx?x=FF2F60195B21487FA19A8EE7767A206C

It it possible to motify the postback address so I can omit the default.aspx from the client browser?

Since .Net 2.0 (I think) you can manually set the Form's action attribute to whatever you want:

<form id="form1" runat="server" action="/">

You can also do that in code-behind:

form1.Action = "/?" & Request.ServerVariables("QUERY_STRING")

I'm using this ControlAdapter which modify action attribute of FORM element to actual url. It's usefull also for url rewriting.

public class FormRewriteAdapter : System.Web.UI.Adapters.ControlAdapter
    {
        [DebuggerStepThrough()]
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(new RewriteFormHtmlTextWriter(writer));
        }
    }


    public class RewriteFormHtmlTextWriter : HtmlTextWriter
    {

        public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
            : base(writer)
        {
            this.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 ((name == "action"))
            {

                System.Web.HttpContext Context = System.Web.HttpContext.Current;

                if (Context.Items["ActionAlreadyWritten"] == null)
                {
                    value = Context.Request.RawUrl;
                    Context.Items["ActionAlreadyWritten"] = true;
                }
            }
            base.WriteAttribute(name, value, fEncode);
        }

    }

you must register this adapter in App_Browsers directory like this:

<adapter controlType="System.Web.UI.HtmlControls.HtmlForm"  adapterType="MyNamaspace.FormRewriteAdapter" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM