IIS7 url重写与ASP.Net 3.5 SP1 + asp:登录表格不起作用

时间:2009-03-16 13:13:16

标签: asp.net authentication .net-3.5 url-rewriting

这个问题之前有点感动,但不是我想要的答案。

我使用IIS7 URL重写模块重写我的页面,现在我的asp.net登录表单不起作用!!!

在我的母版页上,我有这个(ASP.Net 3.5 SP1功能)......

    if (!String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
    {
        form1.Action = Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
    }

这会使页面回发到当前重写的页面。

然而,我的登录控件只是在没有触发任何事件的情况下回发。因此它没有登录,onlogginerror等事件不会触发,没有!!!

我试过这个......

    if (!String.IsNullOrEmpty(Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
    {
        Login Login1 = LoginView1.FindControl("Login1") as Login;
        if (Login1 != null)
            Login1.DestinationPageUrl = Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
    }

没有用......

请注意我使用CSS友好适配器进行登录控制,甚至尝试在此更改此行...

PostBackOptions options = new PostBackOptions(btn, "", "", false, false, false, clientSubmit, true, login.UniqueID);

为...

PostBackOptions options = new PostBackOptions(btn, "", HttpContext.Current.Request.ServerVariables["HTTP_X_ORIGINAL_URL"], false, false, false, clientSubmit, true, login.UniqueID);

没有用......

请帮助:(

1 个答案:

答案 0 :(得分:1)

抱歉太快了...

如另一主题所述,this web site有解决方案。

基本上添加App_Browser文件,然后创建表单重写文件。

Form.browser:

<browsers>
    <browser refID="Default">
        <controlAdapters>
            <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="FormRewriterControlAdapter" />
        </controlAdapters>
    </browser>

</browsers>

FormRewriter.cs:

using System.Web;
using System.Web.UI;

public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
{
    protected override void Render(System.Web.UI.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 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 = default(HttpContext);
            Context = HttpContext.Current;

            if (Context.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 = 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);
    }
}