简体   繁体   中英

What is the return type to redirect to an external page in ASP.NET MVC?

This is mostly linked to my earlier question but is different than that. I am integrating a payment gateway in my code and I have got a post URL to post to. So on click of a button the control goes to the controller from my view where I am calling a method to do the post.

So my concern is I am currently using the below code for posting to the URL however the provider is not ready so I am not able to test but I want to know if I am using the correct return type here.

Will this redirect me to the provider website/page from the controller?

What is the correct way of doing it?Some where I ready I can only do it from Javascript and not from controller.

Can body please clarify I have been struggling with this for the past few days?

public HttpWebResponse SendPostRequest(string data, string url)
{
    var datetime = DateTime.UtcNow;
    data = string.Format("ID*1100|Field01*19101|FirstName*james|LastName*MEZE|AmountDue*20000|CurrentTime*7/5/2016 4:25 PM);
    var requestPayload = Encrypt(data);
    url = "https://www.example.com/Account/SSO/Home";

    HttpWebRequest httpRequest = (HttpWebRequest) HttpWebRequest.Create(url);
    httpRequest.Method = "POST";
    httpRequest.ContentType = "text/plain";// "application/x-www-form-urlencoded";
    httpRequest.ContentLength = encryptedRequestPayload.Length;

    var streamWriter = new StreamWriter(httpRequest.GetRequestStream());
    streamWriter.Write(encryptedRequestPayload);
    streamWriter.Close();

    /*var response = (HttpWebResponse)httpRequest.GetResponse(); 
    var statusCode = response.StatusCode; 
    var description = response.StatusDescription;

    var sr = new StreamReader(response.GetResponseStream());
    var text = sr.ReadToEnd();*/

    return (HttpWebResponse) httpRequest.GetResponse();
}

Returning the HttpWebResponse is only showing me a blank page with the line System.Net.HttpWebResponse. But in the response I am seeing the XML message with the response saying the service is down contact adminstrators. Why am I not able to see that message in the web page? please help.

To redirect to an external web page you can use the Redirect method and pass the url.

public ActionResult SendPostRequest(string data, string url)
{
    //do youre stuff

    return Redirect("https://www.example.com/Account/SSO/Home");
}

That will only work for HTTP GET calls because the browser do a new GET when they become a redirects result.

If you want to POST to another page and show the result page to your visitor, I think the only possible way to achieve this is using JavaScript. For this you can return some simple html with a <script> tag which sends a <form> with HTTP POST.

//$ is the C#6 syntax of string.Format
return Content($@"
    <form action='https://www.example.com/Account/SSO/Home' id='myForm' method='post'>
      <input type='hidden' name='value1' value='{value1}' />
      <input type='hidden' name='value2' value='{value2}' />
    </form>
    <script>
      document.getElementById('myForm').submit(); //submit the form
    </script>");

I got the idea with a form from this answer

You seem to have 2 problems:

1) The server is not responding the way you want. You probably will have to contact the admin as the message says. 2) You are not returning something the ASP.NET MVC is expecting so the ToString() method called by the framework will only display System.Net.HttpWebResponse .

You are close to it though. Shouldn't you be returning the text you get from the response stream?

You can do so by writing:

return Content(text); // after uncommenting your code.

This answer will show you a successful attempt at posting data to a server and passing the response to the client .

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