简体   繁体   中英

How can I redirect to a website on a remote server using server side code in an MVC web application?

I am trying to create an ASP.NET MVC web application which connects to a certain website, which I will call website1, on a certain server, which I will call server A, and from this server the user is able to connect to another website, which I will call website2, on a different server, which I will call server B. Server B and any website therein is only accessible from Server A so the redirect from website1 to website2 must be done using server side code. The user needs to be able to initiate the connection to website2 using something similar to a button or a hyperlink.

I am aware that code within a code block of a view which is denoted by @ does run on the server side, but I cannot get code like this to be called in response to user interaction.The OnClick() handler for the <a></a> tag does not let me call a helper method of the view which might run on the server and neither does @Html.ActionLink() . I can call a method of the controller using @Html.ActionLink("caption","method name") though I cannot figure out how to write server side code here which will connect me to website2. Server.Transfer does not work because this only works for redirecting to pages on the same server. Response.Redirect is also out because this uses the client's browser through which server B and website2 would not be accessible.

I would greatly appreciate any direction on resolving this matter as I do not quite know where to go from here. I have been doing searches extensively but have seen nothing which seems to fit with my situation. Please let me know if I have missed anything and I will gladly check it out.

You can use this anywhere in your controller:

Response.Redirect("http://www.anyserveryoulike.org");

For example, in the action you reach with your:

@Html.ActionLink("caption","method name")

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