我正在編寫一個簡單的URL縮寫器。RedirectPermanent未重定向到網址
除重定向外,一切正常,
這是一種嘗試重定向代碼:
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);
謝謝!
又是什麼'LongUrl'返回? –
@DovydasSopa longUrl返回www.google.com –
如果LongUrl包含http://,它將起作用。有沒有辦法引起它,而不是檢查網址是否包含http://? –