簡體   English   中英

RedirectPermanent不重定向到URL

[英]RedirectPermanent not redirecting to the url

我正在編寫一個簡單的URL縮短器。

除重定向外,一切都正常。

這是嘗試重定向的代碼:

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);
    }

當我輸入縮短的鏈接(例如http:// localhost:41343 / 5d8a2a)時 ,它會將我重定向到http:// localhost:41343 / www.google.com.br,而不是www.google.com.br。

編輯

檢查答案后,它可以工作。 這是最后的代碼片段。

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);

謝謝!

代替RedirectPermanent()嘗試使用Redirect()如下所示。 指定的URL必須是絕對URL,否則它將嘗試重定向到您的應用程序中。

您可以檢查http://存在並相應地添加

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

(要么)

使用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