重定向(URL)无法正常工作

编程入门 行业动态 更新时间:2024-10-24 18:16:46
本文介绍了重定向(URL)无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在ASP MVC3的背景下,我有一个控制器动作,它试图重定向到一个特定的URL此行code的。

In context of ASP MVC3, I have this line of code in a controller action, which tries to redirect to a particular url.

return Redirect(returnUrl);

RETURNURL是包含/首页/索引/的字符串。出于某种原因,重定向不会发生,我仍然在同一屏幕上。我试着删除了结尾的斜线,但没有成功。任何想法,为什么重定向不会发生?

returnUrl is a string that contains "/Home/Index/". For Some reason the redirect is not taking place and I remain on the same screen. I tried removing the trailing slash but no success. Any ideas why the Redirect does not take place?

推荐答案

的重定向法的目的是用来重定向到您的网站的外部URL,并通过将其绝对URL。如果您需要重定向到属于你的网站会得到更好的使用该另一个控制器动作:

The Redirect method is intended to be used to redirect to external urls of your site and by passing it an absolute url. If you need to redirect to another controller action that belongs to your site it would be better to use this:

return RedirectToAction("Index", "Home");

这样,你不再是硬编码网址和code是路由变化不那么脆弱。

This way you are no longer hardcoding urls and your code is less fragile to route changes.

这是说,如果你调用控制器动作执行此与AJAX重定向你不能指望它来随时随地浏览器重定向=>这显然会留在同一页上。 AJAX请求将成功以下所有重定向和成功的回调,您将获得好像是要求没有AJAX /首页/指数网​​址的最终HTML

This being said, if you are invoking the controller action that performs this redirect with AJAX you cannot expect it to redirect the browser anywhere => it will obviously stay on the same page. The AJAX request will succeed following all redirects and in the success callback you will get the final HTML of the /Home/Index url as if it was requested without AJAX.

如果你想在一个AJAX的成功回调重定向叫你可以有例如您的控制器动作返回一个JSON对象,指示要重定向到目标网址:

If you want to redirect in the success callback of an AJAX call you could have your controller action return for example a JSON object indicating the target url you want to redirect to:

return Json(new { redirectToUrl = Url.Action("Index", "Home") });

和在回调中使用 window.location.href 功能:

success: function(result) { window.location.href = result.redirectToUrl; }

更多推荐

重定向(URL)无法正常工作

本文发布于:2023-10-12 17:24:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   重定向   工作   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!