简体   繁体   English

RedirectPermanent不重定向到URL

[英]RedirectPermanent not redirecting to the url

I'm coding a simple URL shortener. 我正在编写一个简单的URL缩短器。

Everything is working, except the redirection. 除重定向外,一切都正常。

Here is the code that tries to redirect: 这是尝试重定向的代码:

public async Task<ActionResult> Click(string segment)
    {
        string referer = Request.UrlReferrer != null ? Request.UrlReferrer.ToString() : string.Empty;
        Stat stat = await this._urlManager.Click(segment, referer, Request.UserHostAddress);
        return this.RedirectPermanent(stat.ShortUrl.LongUrl);
    }

When I input a link that is shortened, like this http://localhost:41343/5d8a2a , it redirects me to http://localhost:41343/www.google.com.br instead of www.google.com.br. 当我输入缩短的链接(例如http:// localhost:41343 / 5d8a2a)时 ,它会将我重定向到http:// localhost:41343 / www.google.com.br,而不是www.google.com.br。

EDIT 编辑

After checking the answer, it works. 检查答案后,它可以工作。 Here is the final snippet of code. 这是最后的代码片段。

if (!stat.ShortUrl.LongUrl.StartsWith("http://") && !stat.ShortUrl.LongUrl.StartsWith("https://"))
            return this.RedirectPermanent("http://" + stat.ShortUrl.LongUrl);
        else
            return this.RedirectPermanent(stat.ShortUrl.LongUrl);

Thanks! 谢谢!

Instead of RedirectPermanent() try using Redirect() like below. 代替RedirectPermanent()尝试使用Redirect()如下所示。 The specified URL has to be a absolute URL else it will try to redirect to within your application. 指定的URL必须是绝对URL,否则它将尝试重定向到您的应用程序中。

You can check for existence of http:// and add it accordingly 您可以检查http://存在并相应地添加

if(!stat.ShortUrl.LongUrl.Contains("http://"))
  return Redirect("http://" + stat.ShortUrl.LongUrl);

(OR) (要么)

Use StartsWith() string function 使用StartsWith()字符串函数

if(!stat.ShortUrl.LongUrl.StartsWith()("http://"))
  return Redirect("http://" + stat.ShortUrl.LongUrl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM