Change HtmlForm action in C# ASP.NET 3.5

后端 未结 6 1346
醉酒成梦
醉酒成梦 2021-01-18 15:11

I have a form as

When accessing in C# code-behind via



        
相关标签:
6条回答
  • 2021-01-18 15:51

    May be you can try redirecttoaction

    Here is sample

    public ActionResult LogOff() {
    FormsAuth.SignOut();
    return RedirectToAction("Index", "Home");
    }
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-18 15:54

    That's just not the way ASP.NET works. You can selectively set certain controls to post to a different page, however. This is called Cross Page Posting. See: http://msdn.microsoft.com/en-us/library/ms178139(v=vs.100).aspx. To perform a cross page postback with a button for example see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx. Basically, you simply set the PostBackUrl for the button.

    0 讨论(0)
  • 2021-01-18 16:03

    You can change the form action like this:

    protected void Page_Init(object sender, EventArgs e)
    {
        Form.Attributes.Add("action", "/Registration/Signup.aspx");
    }
    
    0 讨论(0)
  • 2021-01-18 16:04

    You can use the Control Adapters of asp.net.

    Here is a working example:

    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")
            {
                value = "Change here your value"            
            }
    
            base.WriteAttribute(name, value, fEncode);
        }
    }
    

    With the above code, and a declare on the App_Browsers with a file called Form.browser

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

    you can change the form. Of course this code called in every form render.

    Relative : http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

    0 讨论(0)
  • 2021-01-18 16:05

    There is a class that I found in a KB article that helps achieve this, however, the article has been deleted it seems (originally here: http://www.codeproject.com/Articles/23033/Change-your-ASP-NET-Form-s-Action-attribute-with-R).

    What follows is the class mentioned in that article. Basically if you use the following class and call the static SetFormAction(string url) method on it, you'll be able to set the <form action="url" /> attribute.

    using System.IO;
    using System.Text.RegularExpressions;
    using System.Web;
    
    /// 
    /// The purpose of this class is to easily modify the form "action" of a given asp.net page.
    /// To modify the action, call the following code in the code-behind of your page (or, better, your MasterPage):
    /// Copied (and modified) from http://www.codeproject.com/KB/aspnet/ASP_Net_Form_Action_Attr.aspx
    /// 
    public class FormActionModifier : Stream
    {
      private const string FORM_REGEX = "(]*>)";
      private Stream _sink;
      private long _position;
      string _url;
      public FormActionModifier(Stream sink, string url)
      {
        _sink = sink;
        _url = string.Format("$1{0}$3", url);
      }
    
      public override bool CanRead
      {
        get { return true; }
      }
    
      public override bool CanSeek
      {
        get { return true; }
      }
    
      public override bool CanWrite
      {
        get { return true; }
      }
    
      public override long Length
      {
        get { return 0; }
      }
    
      public override long Position
      {
        get { return _position; }
        set { _position = value; }
      }
    
      public override long Seek(long offset, System.IO.SeekOrigin direction)
      {
        return _sink.Seek(offset, direction);
      }
    
      public override void SetLength(long length)
      {
        _sink.SetLength(length);
      }
    
      public override void Close()
      {
        _sink.Close();
      }
    
      public override void Flush()
      {
        _sink.Flush();
      }
    
      public override int Read(byte[] buffer, int offset, int count)
      {
        return _sink.Read(buffer, offset, count);
      }
    
      public override void Write(byte[] buffer, int offset, int count)
      {
        string s = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
        Regex reg = new Regex(FORM_REGEX, RegexOptions.IgnoreCase);
        Match m = reg.Match(s);
        if (m.Success)
        {
          string form = reg.Replace(m.Value, _url);
          int iform = m.Index;
          int lform = m.Length;
          s = string.Concat(s.Substring(0, iform), form, s.Substring(iform + lform));
        }
        byte[] yaz = System.Text.UTF8Encoding.UTF8.GetBytes(s);
        _sink.Write(yaz, 0, yaz.Length);
      }
    
      /// 
      /// Sets the Form Action to the url specified
      /// 
      public static void SetFormAction(string url)
      {
        if (HttpContext.Current != null)
          HttpContext.Current.Response.Filter = new FormActionModifier(HttpContext.Current.Response.Filter, url);
      } // SetFormAction()
    } // class
    
    0 讨论(0)
  • 2021-01-18 16:15

    I also had the same issue recently, when posting the form action attribute would use the rewritten path, but I wanted to show the raw url. I don't have any url rewriting libraries installed.

    I found this page was very useful in getting my postbacks to work: http://www.codeproject.com/Articles/94335/TIP-How-to-Handle-Form-Postbacks-when-Url-Rewritin

    0 讨论(0)
提交回复
热议问题