怎么在ASP.NET Core項目中實現(xiàn)一個重定向功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
可以使用下面任何一個方法來返回 RedirectResult。
Redirect 返回 Http 狀態(tài)碼為 302
RedirectPermanent 返回 Http 狀態(tài)碼為 301
RedirectPermanentPreserveMethod 返回 Http 狀態(tài)碼為 308
RedirectPreserveMethod 返回 Http 狀態(tài)碼為 307
具體狀態(tài)碼代表什么意思,大家可查專業(yè)資料,下面的代碼展示了如何使用這些方法。
Redirect("/Home/Index"); RedirectPermanent("/Home/Index"); RedirectPermanentPreserveMethod("/Home/Index"); RedirectPreserveMethod("/Home/Index");
如果你被這些方法搞蒙圈了,可以直接使用 RedirectResult ,然后通過 permanent 和 preserveMethod 兩個參數(shù)去調(diào)節(jié)返回什么樣的 Http 狀態(tài)碼即可, 代碼如下所示:
public RedirectResult Index() { return new RedirectResult(url: "/Home/Index", permanent: true, preserveMethod: true); }
值得注意的是,Redirect 方法也可以將請求導向一個指定的url地址上,比如下面這樣:
public RedirectResult Index() { return Redirect("https://google.com"); }
接下來簡單了解一下繼承關系: HomeController 繼承了 Controller ,后者又繼承了 ControllerBase 并實現(xiàn)了 IActionFilter, IFilterMetadata, IAsyncActionFilter, 和 IDisposable 接口,如下代碼所示:
public class HomeController : Controller { } public abstract class Controller : ControllerBase, IActionFilter, IFilterMetadata, IAsyncActionFilter, IDisposable { }
這個 ActionResult 用于將請求轉(zhuǎn)向到指定的 Controller.Action ,如果沒有指定 Controller 的話,自然就會跳轉(zhuǎn)到當前 Controller 下的 Action,可使用下面羅列的方法將請求跳轉(zhuǎn)到指定的 Action。
RedirectToAction 返回 Http 狀態(tài)碼為 302
RedirectToActionPermanent 返回 Http 狀態(tài)碼為 301
RedirectToActionPermanentPreserveMethod 返回 Http 狀態(tài)碼為 308
RedirectToActionPreserveMethod 返回 Http 狀態(tài)碼為 307
如果不想使用具體的方法,也可以直接使用父類的 RedirectToAction 方法。
public RedirectToActionResult Index() { return RedirectToAction(actionName: "Index", controllerName: "Home"); }
如果你只需要跳轉(zhuǎn)到當前 Controller 下的某一個 Action,可以忽略 Controller 名字,如下代碼所示:
public RedirectToActionResult Index() { return RedirectToAction(actionName: "Privacy"); }
這是另一種可將 請求跳轉(zhuǎn)到指定 Action 的方式,你可以使用下面羅列的方法來實現(xiàn)跳轉(zhuǎn)。
RedirectToRoute 返回 Http 狀態(tài)碼為 302
RedirectToRoutePermanent 返回 Http 狀態(tài)碼為 301
RedirectToRoutePermanentPreserveMethod 返回 Http 狀態(tài)碼為 308
RedirectToRoutePreserveMethod 返回 Http 狀態(tài)碼為 307
下面的代碼片段展示了 如何使用 RedirectToRoute 。
public RedirectToRouteResult Index() { return RedirectToRoute("author"); }
也可以通過 RouteValueDictionary 來指定需要跳轉(zhuǎn)的 Route 值,如下代碼所示:
var routeValue = new RouteValueDictionary(new { action = "View", controller = "Author"}); return RedirectToRoute(routeValue);
這個 ActionResult 只用于跳轉(zhuǎn)到本地url ,也就意味著一旦你跳轉(zhuǎn)到外部網(wǎng)站的url,肯定會拋出異常的??梢允褂孟旅媪_列的方法來實現(xiàn)跳轉(zhuǎn)。
LocalRedirect 返回 Http 狀態(tài)碼為 302
LocalRedirectPermanent 返回 Http 狀態(tài)碼為 301
LocalRedirectPermanentPreserveMethod 返回 Http 狀態(tài)碼為 308
LocalRedirectPreserveMethod 返回 Http 狀態(tài)碼為 307
最后需要了解的一點是,你可以使用 RedirectToPage 方法將請求跳轉(zhuǎn)到指定的 Razor 頁面,返回 Http狀態(tài)碼 302,比如說:你有一個 Author Page,接下來用如下代碼實現(xiàn)跳轉(zhuǎn)。
public IActionResult RedirectToAuthorPage() { return RedirectToPage("Author"); }
關于怎么在ASP.NET Core項目中實現(xiàn)一個重定向功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。
當前標題:怎么在ASP.NETCore項目中實現(xiàn)一個重定向功能-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://jinyejixie.com/article32/hidsc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、品牌網(wǎng)站設計、企業(yè)建站、營銷型網(wǎng)站建設、定制開發(fā)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容