简体   繁体   English

重定向到ASP.NET MVC中的外部页面的返回类型是什么?

[英]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. 我在代码中集成了付款网关,并且有发布URL。 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. 因此,我担心的是我目前正在使用以下代码将其发布到URL,但是提供程序尚未准备就绪,因此我无法进行测试,但是我想知道我是否在这里使用了正确的返回类型。

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. 正确的方法是什么?在某些地方,我只能从Javascript而不是从控制器进行。

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. 返回HttpWebResponse只会显示空白行System.Net.HttpWebResponse。 But in the response I am seeing the XML message with the response saying the service is down contact adminstrators. 但是在响应中,我看到了XML消息,响应中说该服务已关闭,联系管理员。 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. 要重定向到外部网页,您可以使用Redirect方法并传递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. 这仅适用于HTTP GET调用,因为当浏览器成为重定向结果时,浏览器会执行新的GET。

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. 如果您想发布到另一个页面并将结果页面显示给访问者,我认为实现此目的的唯一可能方法是使用JavaScript。 For this you can return some simple html with a <script> tag which sends a <form> with HTTP POST. 为此,您可以返回一些带有<script>标记的简单html,该标记通过HTTP POST发送<form>

//$ 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. 1)服务器没有响应您想要的方式。 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 . 2)您没有返回ASP.NET MVC期望的东西,因此框架调用的ToString()方法将仅显示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 . 此答案将向您展示成功尝试将数据发布到服务器并将响应传递给客户端的尝试。

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

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