Skip to content

Instantly share code, notes, and snippets.

@priore
Created October 25, 2013 23:32
Show Gist options
  • Select an option

  • Save priore/7163408 to your computer and use it in GitHub Desktop.

Select an option

Save priore/7163408 to your computer and use it in GitHub Desktop.
ASP.NET [C#] Redirect with post data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using System.Text;
// ASP.NET [C#] REDIRECT WITH POST DATA
public static class WebExtensions
{
public static void RedirectWithData(NameValueCollection data, string url)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
StringBuilder s = new StringBuilder();
s.Append("<html>");
s.AppendFormat("<body onload='document.forms[\"form\"].submit()'>");
s.AppendFormat("<form name='form' action='{0}' method='post'>", url);
foreach (string key in data)
{
s.AppendFormat("<input type='hidden' name='{0}' value='{1}' />", key, data[key]);
}
s.Append("</form></body></html>");
response.Write(s.ToString());
response.End();
}
}
@ErtugKaya

Copy link
Copy Markdown

This is amazing. Thank you for sharing.

@f0rza

f0rza commented Jan 7, 2016

Copy link
Copy Markdown

Perfect solution! Saved a lot of my time. Thank you so much

@Petryxasport

Copy link
Copy Markdown

how get data form c#?

@iMoggle

iMoggle commented Jun 22, 2016

Copy link
Copy Markdown

This is exactly what I was looking for . Thank you very much for sharing!

@tugceaktepe

Copy link
Copy Markdown

Thank you so much! You saved my time!

@akashdeepshah

Copy link
Copy Markdown

How to post Files using above method?

@ReactRS

ReactRS commented Aug 28, 2017

Copy link
Copy Markdown

Awesome !! Save my time !

@professionalsna

Copy link
Copy Markdown

Thanks for the scripts....

@rasoulian

Copy link
Copy Markdown

Good Solution! Thank you.

@rockybalog

Copy link
Copy Markdown

How do you do this in ASP.Net Core 2 since response.write and response.end are no linger available?

@rockybalog

Copy link
Copy Markdown

Any help please?

@fidelflorian

Copy link
Copy Markdown

has the same code for .net core

@fidelflorian

Copy link
Copy Markdown

@rockybalog that's how it worked for me
HttpContext.Response.WriteHtmlAsync(s.ToString());
"you have to use the same example html"

@ghulamostafa

Copy link
Copy Markdown

Thank you. It is 6 years old but helped me.

@owl3408

owl3408 commented May 28, 2020

Copy link
Copy Markdown

Thank you

@amp838

amp838 commented Jun 15, 2020

Copy link
Copy Markdown

Please note that the "response.End()" at the end may be causing "thread was being aborted" exception. In that case replace it by "HttpContext.Current.ApplicationInstance.CompleteRequest()" as per the instructions in "https://support.microsoft.com/en-us/help/312629/prb-threadabortexception-occurs-if-you-use-response-end-response-redir"

@umerwaleed

Copy link
Copy Markdown

It's not working in Asp.Net MVC

@rodrigocsouza

Copy link
Copy Markdown

Very good, you made my day!
Regards!

@Samtoch

Samtoch commented Mar 6, 2021

Copy link
Copy Markdown

@rockybalog did you later find a work around for this on .net core?
@fidelflorian pls what exactly did you do?

@rockybalog

rockybalog commented Mar 6, 2021 via email

Copy link
Copy Markdown

@Samtoch

Samtoch commented Mar 7, 2021

Copy link
Copy Markdown

Good to know @rockybalog please help me when your find time. Thanks in advance

@TSIHariharan

Copy link
Copy Markdown

Thank you so much. I search this code from a month. Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment