asp.net-mvc 从 ASP.NET MVC 控制器重定向到外部 URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1549324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect to external URI from ASP.NET MVC controller
提问by user135498
I'm trying to redirect to external url from an action method but can't get it to work. Can anybody shed some light on my error?
我正在尝试从操作方法重定向到外部 url,但无法使其正常工作。任何人都可以对我的错误有所了解吗?
public void ID(string id)
{
string url = string.Empty;
switch (id)
{
case "DB2FCB11-579F-4DA2-A68C-A6495B9BAAB5":
url = "http://www.somesite.com";
break;
}
Response.Redirect(url, true);
}
Thanks, Chris
谢谢,克里斯
回答by Yuriy Faktorovich
If you're talking about ASP.NET MVC then you should have a controller method that returns the following:
如果您在谈论 ASP.NET MVC,那么您应该有一个返回以下内容的控制器方法:
return Redirect("http://www.google.com");
Otherwise we need more info on the error you're getting in the redirect. I'd step through to make sure the url isn't empty.
否则,我们需要有关您在重定向中遇到的错误的更多信息。我会逐步确保该网址不为空。
回答by shaijut
Using JavaScript
使用 JavaScript
public ActionResult Index()
{
return Content("<script>window.location = 'http://www.example.com';</script>");
}
Note:As @Jeremy Ray Brown said , This is not the best option but you might find useful in some situations.
注意:正如@Jeremy Ray Brown 所说,这不是最佳选择,但在某些情况下您可能会发现它很有用。
Hope this helps.
希望这可以帮助。
回答by Pedro Henrique Silva
Maybe the solution someone is looking for is this:
也许有人正在寻找的解决方案是这样的:
Response.Redirect("/Sucesso")
This work when used in the View as well.
这在 View 中使用时也有效。
回答by Yilmazam
Try this (I've used Home controller and Index View):
试试这个(我用过主控制器和索引视图):
return RedirectToAction("Index", "Home");

