MVC 路由 及 遇到问题记录
来源:互联网 发布:淘宝网店管理技巧 编辑:程序博客网 时间:2024/05/28 04:53
- MVC 路由
- 重定向
- 问题记录
1)MVC 路由入口方法:(Global.asax)Application_Start()--->(App_Start/RouteConfig.cs)RegisterRoutes()--->(Controllers/)Controller--->(Views/)Action--->(Views/Action/)View其中Application_Start()代码如下:
<span style="font-size:14px;font-weight: normal;"><span style="font-size:14px;">namespace MvcAspxMovie{ public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default",//路由名称 url: "{controller}/{action}/{id}",//带有参数的URL defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }//默认参数 ); } }}</span></span>
2)重定向:页面跳转传参(RedirectToAction())
在MVC中页面后台中常用的页面跳转方法有几种,如:return View()、return RedirectToAction(). 一般情况下我们返回的都是本页面,所以使用return View()就够了.有时候我们也会遇到返回的页面不是本页面的,就要用到return RedirectToAction();方法了。举例如下,通过RedirectToAction调用跳转到HelloWorld(),页面返回“helloworld”。 public string HelloWorld() { return "hello world"; } public ActionResult TestRedirect() { return RedirectToAction("HelloWorld"); }
遇到需要传参的情况,则需要对RedirectToAciton()扩展RedirectToAction(string ActionName); //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">跳转到同一Controller 里面的不同Action</span>RedirectToAction(string ActionName, object viewData); //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">跳转到同一Controller 里面的不同Action,含参数</span>RedirectToAction(string ActionName, string ControllerName); //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">跳转到不同Controller 里面的不同Action</span>RedirectToAction(string ActionName, string ControllerName, object viewData); //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">跳转到不同Controller 里面的不同Action,含参数</span>
<span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;">如:RedirectToAction("Index","Home",new {msg="操作成功",name="admin"});</span></span>
<%: Html.ActionLink("Edit", "Edit", new { id=item.ID }) %>
举例如下,调用TestRedirect()最终页面返回“hello bailang” public string HelloWorld(string str) { return "hello "+str; } public ActionResult TestRedirect() { return RedirectToAction("HelloWorld",new{str="bailang"}); }
3)问题记录20160104 初学MVC 遇到新建一个controller,默认action 是index,URL输入 http://localhost:15683/TestThumbnail 默认为http://localhost:15683/TestThumbnail/index,但未知修改会造成再次URL输入默认到index不起作用,必须定位输入http://localhost:15683/TestThumbnail/index,原因发现为:在根目录下生成同名controller的目录。解决方法:删除该目录即可。
<span style="font-size:14px;font-weight: normal;"><span style="font-size:14px;">namespace MvcAspxMovie{ public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default",//路由名称 url: "{controller}/{action}/{id}",//带有参数的URL defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }//默认参数 ); } }}</span></span>
2)重定向:页面跳转传参(RedirectToAction())
在MVC中页面后台中常用的页面跳转方法有几种,如:return View()、return RedirectToAction().
一般情况下我们返回的都是本页面,所以使用return View()就够了.有时候我们也会遇到返回的页面不是本页面的,就要用到return RedirectToAction();方法了。
举例如下,通过RedirectToAction调用跳转到HelloWorld(),页面返回“helloworld”。
public string HelloWorld() { return "hello world"; } public ActionResult TestRedirect() { return RedirectToAction("HelloWorld"); }
遇到需要传参的情况,则需要对RedirectToAciton()扩展
RedirectToAction(string ActionName); //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">跳转到同一Controller 里面的不同Action</span>RedirectToAction(string ActionName, object viewData); //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">跳转到同一Controller 里面的不同Action,含参数</span>RedirectToAction(string ActionName, string ControllerName); //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">跳转到不同Controller 里面的不同Action</span>RedirectToAction(string ActionName, string ControllerName, object viewData); //<span style="font-family: Arial; font-size: 14px; line-height: 26px;">跳转到不同Controller 里面的不同Action,含参数</span>
<span style="font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px;">如:RedirectToAction("Index","Home",new {msg="操作成功",name="admin"});</span></span>
<%: Html.ActionLink("Edit", "Edit", new { id=item.ID }) %>举例如下,调用TestRedirect()最终页面返回“hello bailang”
public string HelloWorld(string str) { return "hello "+str; } public ActionResult TestRedirect() { return RedirectToAction("HelloWorld",new{str="bailang"}); }
0 0
- MVC 路由 及 遇到问题记录
- Canvas学习记录及遇到的问题
- 首个spring mvc 测试例子搭建遇到问题记录
- MVC 控制器和 路由,及路由调试
- spring mvc学习中遇到的问题及解决办法
- spring MVC 学习遇到的问题及解决方案汇总
- 记录arch 使用中的遇到一些问题及解决方法
- ImageView设置selector遇到的小问题及解决方案记录
- android studio2.0遇到的问题及解决方案记录
- 在virtualbox中安装ubuntu遇到问题及方法记录
- git中遇到的一些问题及解决方法记录
- 做android 开发遇到的问题及解决办法记录
- ssh整合中遇到的问题及解决记录
- 记录网页制作过程中遇到的问题及解决方案
- 记录一下本周的主要工作及遇到的问题
- 新手教程 Centos7安装二进制mysql 及遇到问题记录
- android studio遇到的问题及解决方案记录
- 自己开始学习到遇到问题及解决方法的记录
- Redis五种数据类型介绍
- Google Map开发系列(二)——使用谷歌地图API之前应该有所了解的一些技术外背景
- [从头学数学] 第27节 总复习
- swift安装cocoapod遇到的一些坑
- python中常用的MySQL操作以及命令
- MVC 路由 及 遇到问题记录
- Shiro web 和spring
- objective-c dictionary(字典)
- 静态分派与动态分派
- 打开 Android Studio unable to access android sdk add-on list问题
- ArrayList.add(int index, E e)的数组越界
- Boost库的使用
- Java中的数字处理
- opengl基础知识 关于四个变换的顺序