简体   繁体   English

更改回发URL以隐藏Default.aspx

[英]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 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 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? 可以装饰回发地址,以便可以从客户端浏览器中省略default.aspx吗?

Since .Net 2.0 (I think) you can manually set the Form's action attribute to whatever you want: 从.Net 2.0(我认为)开始,您可以将Form的action属性手动设置为所需的任何值:

<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. 我正在使用此ControlAdapter,它将FORM元素的action属性修改为实际的url。 It's usefull also for url rewriting. 这对于URL重写也很有用。

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: 您必须像这样在App_Browsers目录中注册此适配器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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