【问题标题】:Redirect to Different Url From Asp.net mvc3 Controller从 Asp.net mvc3 控制器重定向到不同的 URL
【发布时间】:2012-05-19 19:42:51
【问题描述】:

我正在使用 asp.net mvc3 并且有一个问题,我想将用户重定向到用户输入的任何动态链接,如何做到这一点,我目前的工作正在跟进

我尝试了两种类型

1.

控制器

 if (condition == true)
        {
            string Link ="www.google.com";  // suppose this is the url entered by user
            ViewData["link"] = Link;
            return PartialView("REdirectToLink");
         }

REdirectToLink.cshtml

@{
  string link = ViewData["link"].ToString(); 
 }
 <script type="text/javascript" >
 var tlink = "@link";
 window.location.replace(tlink);
 </script>

2.

控制器

if (condition == true)
        {              
           string Link ="www.google.com"; // suppose this is the url entered by user
            return  RedirectPermanent(Link);// also tried return  Redirect(Link);
         }

如何做到这一点,在此先感谢!

【问题讨论】:

  • 您遇到了什么问题?重定向不起作用,还是您在询问是否有更好的方法来做到这一点?如果是后者,我认为您的第二种解决方案优于第一种。
  • 假设我当前的位置是 "example.com/foo" ,现在当我调用 Redirect 时,它会输入这样的 url "example.com/www.google.com"

标签: c# asp.net-mvc asp.net-mvc-3 c#-4.0


【解决方案1】:

只需使用:

string link = "http://www.google.com";
return Redirect(link);

【讨论】:

  • 意思...假设我当前的位置是 "example.com/foo" ,现在当我调用 Redirect 时,它会输入这样的网址 "example.com/www.google.com"
  • 你把“http://”放在网址的开头吗?如果你不这样做,它看起来像一个相对 url
  • 天啊! ...当我包含“http://”时它可以工作...谢谢@Bond ....请将此作为答案
  • 其实我没有注意到,但 Veblock 还包括 "http://" 。感谢@Bond 和 veblock
  • 您还可以使用 RedirectPermanent("/Something"); 重定向相对 URL例如。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
相关资源
最近更新 更多