mvc重定向方式詳解
本文實(shí)例為大家分享了mvc重定向的幾種方式,供大家參考,具體內(nèi)容如下
在RouteConfig添加一個(gè)簡(jiǎn)單的路由
//新增路由
routes.MapRoute(
name: "Article",
url: "Detial/{id}",
defaults: new { controller = "Article", action = "Detial", id = UrlParameter.Optional },
constraints: new { id = @"\d+" }
//namespaces: new string[] { }
);
302重定向
public ActionResult UrlTest1()
{//302
return Redirect("/Article/Detial/1");
}
public ActionResult UrlTest2()
{//302
return RedirectToAction("Detial", "Article", new System.Web.Routing.RouteValueDictionary(new { id = 2 }));
//return RedirectToAction("Detial", "Article",new { id = 1});
}
public ActionResult UrlTest3()
{//302
return RedirectToRoute("Article", new System.Web.Routing.RouteValueDictionary(new { id = 3 }));
//return RedirectToRoute("Article", new { id = 1 });
}
301重定向
public ActionResult UrlTest4()
{//301
return RedirectPermanent("/Article/Detial/4");
}
public ActionResult UrlTest5()
{//301
return RedirectToActionPermanent("Detial", "Article", new System.Web.Routing.RouteValueDictionary(new { id = 5 }));
//return RedirectToActionPermanent("Detial", "Article", new { id = 1 });
}
public ActionResult UrlTest6()
{//301
return RedirectToRoutePermanent("Article", new System.Web.Routing.RouteValueDictionary(new { id = 6 }));
//return RedirectToRoutePermanent("Article", new { id = 1 });
}
也可以自己設(shè)置
public ActionResult UrlTest7()
{//可設(shè)置
return new RedirectToRouteResult("Article", new System.Web.Routing.RouteValueDictionary(new { id = 7 }), false) { };
}
public ActionResult UrlTest8()
{//可設(shè)置
return new RedirectResult("/Article/Detial/8", false);
}
要注意的是,在View()中指定不同的視圖不是重定向
public ActionResult UrlTest9()
{//200
return View("Detial", null, new { id = 9 });
}
第二個(gè)代碼段和第三個(gè)代碼段中的方法,都會(huì)用第四個(gè)代碼段中的形式最后以Response.Redirect方法返回給客戶端
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET MVC頁面重定向簡(jiǎn)單介紹
- 詳解SpringMVC重定向傳參數(shù)的實(shí)現(xiàn)
- ASP.NET MVC3 實(shí)現(xiàn)全站重定向的簡(jiǎn)單方法
- asp.net RewritePath重定向HTTP頭Content-Location暴露真實(shí)路徑解決方法
- Asp.Net實(shí)現(xiàn)404頁面與301重定向的方法
- Windows虛擬主機(jī)與VPS如何實(shí)現(xiàn)301重定向(asp.net)
- 301重定向代碼合集(iis,asp,php,asp.net,apache)
- asp.net php asp jsp 301重定向的代碼(集合)
- Asp.Net 重定向必須要知道的一些資料
- ASP.NET 重定向的幾種方法小結(jié)
相關(guān)文章
ASP.NET?MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息
這篇文章介紹了ASP.NET?MVC遍歷ModelState錯(cuò)誤信息的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
.NET 6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)API版本控制
API接口版本管理,對(duì)于一些規(guī)模稍大的企業(yè)應(yīng)用來說,是經(jīng)常需要關(guān)注的一大需求。本文將介紹在.NET 6開發(fā)中如何實(shí)現(xiàn)API版本控制,感興趣的可以了解一下2022-01-01
ASP.NET數(shù)據(jù)庫編程之處理文件訪問許可
ASP.NET數(shù)據(jù)庫編程之處理文件訪問許可...2006-09-09
.NET發(fā)送郵件的實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于.NET發(fā)送郵件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.net具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
ASP.NET 前臺(tái)javascript與后臺(tái)代碼調(diào)用
ASP.NET中前臺(tái)javascript與后臺(tái)代碼調(diào)用的實(shí)現(xiàn)代碼說明。2009-08-08
asp.net 判斷數(shù)組是否存在某個(gè)值的方法
asp.net 判斷數(shù)組是否存在某個(gè)值的兩種方法, 需要的朋友可以參考下。2010-07-07
Asp.net中安全退出時(shí)清空Session或Cookie的實(shí)例代碼
網(wǎng)站中點(diǎn)擊退出,如果僅僅是重定向到登錄/出頁面,此時(shí)在瀏覽器地址欄中輸入登錄后的某個(gè)頁面地址如主頁,你會(huì)發(fā)現(xiàn)不用登錄就能訪問,這種退出并不安全了,下面通過本文給大家介紹安全退出時(shí)清空Session或Cookie的實(shí)例代碼2016-11-11

