MVC2: 路由 及 遇到问题记录

  1. MVC 路由
  2. 重定向
  3. 问题记录
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的目录。
解决方法:删除该目录即可。


 

转载于:https://www.cnblogs.com/bailang880608/p/5123224.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值