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方法返回給客戶(hù)端
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET MVC頁(yè)面重定向簡(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頁(yè)面與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)文章
ajaxToolkit:TextBoxWatermarkExtender演示與實(shí)現(xiàn)代碼
該控件的效果就是在TextBox控件上添加“水印”效果,也就是當(dāng)TextBox為空時(shí),顯示提示消息,一旦TextBox聚焦,樣式就消失,看起來(lái)還挺不錯(cuò)的嗎,感興趣的你可以了解下哦,希望本文對(duì)你有所幫助2013-01-01
asp.net中Post表單保存頁(yè)面狀態(tài)并輸出源碼的實(shí)現(xiàn)方法
先執(zhí)行腳本,復(fù)制源碼到隱藏域里,再輸出源碼,注意代碼紅色設(shè)置2012-08-08
詳解c# .net core 下的網(wǎng)絡(luò)請(qǐng)求
本篇文章主要介紹了詳解c# .net core 下的網(wǎng)絡(luò)請(qǐng)求,大致介紹下在.net core 下如何進(jìn)行http請(qǐng)求,主要仍然是GET和POST方法,有興趣的可以了解下2017-05-05
解決ASP.NET中Type.GetType方法總返回空的問(wèn)題
今天做練習(xí)的時(shí)候用到了Type.GetType方法。。 可是他總返回null。。2008-03-03
asp.net button 綁定多個(gè)參數(shù)
asp.net button 綁定多個(gè)參數(shù)的代碼2008-11-11
ASP.NET 修復(fù) IIS 映射具體實(shí)現(xiàn)步驟
本文主要介紹IIS映射的具體步驟,希望對(duì)大家有所幫助。2016-05-05
asp.net下DataSet.WriteXml(String)與(Stream)的區(qū)別
asp.net下DataSet.WriteXml(String)與(Stream)的區(qū)別...2007-04-04

