mvc重定向方式详解
程序员文章站
2023-11-18 09:18:10
本文实例为大家分享了mvc重定向的几种方式,供大家参考,具体内容如下
在routeconfig添加一个简单的路由
//新增路由
routes.maprout...
本文实例为大家分享了mvc重定向的几种方式,供大家参考,具体内容如下
在routeconfig添加一个简单的路由
//新增路由 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 }); }
也可以自己设置
public actionresult urltest7() {//可设置 return new redirecttorouteresult("article", new system.web.routing.routevaluedictionary(new { id = 7 }), false) { }; } public actionresult urltest8() {//可设置 return new redirectresult("/article/detial/8", false); }
要注意的是,在view()中指定不同的视图不是重定向
public actionresult urltest9() {//200 return view("detial", null, new { id = 9 }); }
第二个代码段和第三个代码段中的方法,都会用第四个代码段中的形式最后以response.redirect方法返回给客户端
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。