浅谈mvc3

1、MVC的路由配置

      routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

上面代码表示当你当你输入http://localhost:*/时会默认找你的http://localhost:*/home/index这里的配置只允许你传一个参数也就是home/index/{参数}或者home/index?id={参数},现在考虑一下当传递多个参数时该怎么办

   routes.MapRoute(
          "Default", // 路由名称
          "{controller}/{action}/{id}/{name}", // 带有参数的 URL
          new { controller = "Home", action = "Index", id = UrlParameter.Optional,name=UrlParameter.Optional } // 参数默认值
      );

上面代码显示了当传递两个参数时应该是这样home/index/{id}/{name}或者home/index?id={id}&name={name}且这两个参数是可选的

2、mvc的网页引擎由原来的.aspx换成现在razor

网页上的所有操作都显示的非常方便control可以像view层传递强显示类型的累传递的方式

public class user{
public string username{get;set;}//用户名
public string password{get;set;}//密码
}
public ActionResult Index()
        {
          user u=new user{username="zhansgsa",password="hello"}
            return View(u );
        }

前台接收的方式是

@model MvcApplication15.Controllers.HomeController.user
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<p>@Model.username</p>
<p>@Model.password</p>

所有的操作变得都非常方便,razor引擎使得对网页的操作由以前的<%%>变为现在@就直接操作

@if (Model.username!=null)
{
    while (Model.username != null)
    {
        
    }
}

等等的操作都非常的方便

posted @ 2012-10-20 22:41  win_and_first  阅读(274)  评论(0编辑  收藏  举报