![](/img/trans.png)
[英]Redirect from asp.net page to another using javascript function
[英]How do I redirect to another web page from an ASP.NET Controller using JavaScript?
我正在使用 ASP.NET MVC 后端開發 Angular 1 網站。 I need a link to gather some parameters using JavaScript, then go to a controller to get the correct URL, then send the user to a different website. 但是,我嘗試的任何方法都不起作用。
如果重要的話,這是 HTML:
<a href="#" target="_blank" id="Link">Some text</a>
這是我的 JavaScript:
$("#Link").on('click', function (event) {
event.preventDefault();
window.location = "/Data/SendToOtherSite?Name=" + $("#nameTextBox").val() + "&Email=" + $("#emailTextBox").val();
});
這是我的 controller 上的方法:
public ActionResult SendToOtherSite(string Name, string Email)
{
string url = System.Web.Configuration.WebConfigurationManager.AppSettings["OtherSiteUrl"]
+ "/Data/DataFromOldSite?name=" + Name + "&email=" + Email;
return Redirect(url);
}
我在 SendToOtherSite() 上有一個斷點,但它從未被擊中,我也沒有被帶到新網站。 我需要做什么?
嘗試以下操作:
$("#Link").click(function (event) {
event.preventDefault();
window.location.href = '@Url.Action("SendToOtherSite", "Data")?name=' + $("#nameTextBox").val() + '&email=' + $("#emailTextBox").val();
});
操作方法參數應正確格式化,以便能夠被 MVC 標准綁定過程正確解析。 因此,應該使用@Url.Action()
來構建正確的路線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.