ASP.Net MVC(3)

来源:互联网 发布:淘宝账号名怎么改 编辑:程序博客网 时间:2024/05/16 13:43

1表单提交到控制器的时候,执行控制器的Action之前会自动将表单中的内容填充到方法的参数或者参数的属性里面去

2.Razor试图引擎,文件类型,cshtml和vbhtml,可以使用@{code}来定义一段代码块

HtmlString类型和MvcHtmlString类型字符输出

@Html.Raw


Razor引擎的转换数据类型
AsInt()把字符串转换为整数。
if (myString.IsInt())
IsInt() {myInt=myString.AsInt();}
AsFloat()
把字符串转换为浮点数。
if (myString.IsFloat())
IsFloat() {myFloat=myString.AsFloat();}
AsDecimal()
把字符串转换为十进制数。
if (myString.IsDecimal())
IsDecimal() {myDec=myString.AsDecimal();}
AsDateTime()
把字符串转换为 ASP.NET DateTime
类型
myString="10/10/2012";
IsDateTime() myDate=myString.AsDateTime();
AsBool()
把字符串转换为逻辑值。
myString="True";
IsBool() myBool=myString.AsBool();
ToString() 把任意数据类型转换为字符串。
myInt=1234;
myString=myInt.ToString();

3.表单数据自动装配到Action方法的参数里,也可以装配到实体类,也是数据绑定

Razor服务器端注释为:@* 注释内容*@

在Action中可以访问HttpContext中所有的相关数据:比如Session、Cookie、也可以设置响应总之跟WebFormPage类能做的,在Action中都能做,控制器的Action中既可以操作请求也可以操作响应、


    ActionResult派生类:类名 抽象类 父类 功能ContentResult     根据内容的类型和编码,数据内容.EmptyResult     空方法.FileResult abstract   写入文件内容,具体的写入方式在派生类中.FileContentResult   FileResult 通过 文件byte[] 写入文件.FilePathResult   FileResult 通过 文件路径 写入文件.FileStreamResult   FileResult 通过 文件Stream 写入文件.HttpUnauthorizedResult     抛出401错误JavaScriptResult     返回javascript文件JsonResult     返回Json格式的数据RedirectResult     使用Response.Redirect重定向页面RedirectToRouteResult     根据Route规则重定向页面ViewResultBase abstract   调用IView.Render()PartialViewResult   ViewResultBase 调用父类ViewResultBase 的ExecuteResult方法.      重写了父类的FindView方法.      寻找用户控件.ascx文件ViewResult   ViewResultBase 调用父类ViewResultBase 的ExecuteResult方法.      重写了父类的FindView方法.      寻找页面.aspx文件    
对于控制器,Action中返回的是一系列ResultAction的子类

4.路由

Routing的作用:
—确定Controller
—确定Action
—确定其他参数
—根据识别出来的数据, 将请求传递给
Controller和Action


调试路由RouteDebug,应用routedebug.dll,在application_start中能看到路由的解析结果

RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
注册路由的重载方法

MapRoute( string name, string url);
MapRoute( string name, string url, object defaults);
MapRoute( string name, string url, string[] namespaces);

MapRoute( string name, string url, object defaults, object constraints);
MapRoute( string name, string url, object defaults, string[]namespaces);
 MapRoute( string name, string url, object defaults, object constraints,string[] namespaces)


常见首页路由规则,

name 参数:
 规则名称, 可以随意起名.不可以重名,否则会发生错误:路由集合中已经存在名为 “Default” 的路由。路由名必须
是唯一的。
url 参数:url获取数据的规则, 这里不是正则表达式, 将要识别的
参数括起来即可, 比如: {controller}/{action} 最少只需要传递name和url参数就可以建立一条
Routing(路由)规则.比如实例中的规则完全可以改为: routes.MapRoute( "Default", "{controller}/{action}");

defaults 参数:
url参数的默认值.如果一个url只有controller: localhost/home/ 而且我们只建立了一条url获取数据规则: {controller}/{action}
 那么这时就会为action参数设置defaults参数中规定的默认值. defaults参
数是Object类型,所以可以传递一个匿名类型来初始化默认值: new { controller = "Home", action = "Index" } 实例中使用的是三个参数
的MapRoute方法: routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", //
URL with parameters new { controller = "Home", action = "Index", id =
"" } // Parameter defaults );


constraints 参数:
用来限定每个参数的规则或Http请求的类型.constraints属性是一个RouteValueDictionary对象,也就是一个字典表, 但是这个字典
表的值可以有两种:用于定义正则表达式的字符串。正则表达式不区分大小写。
一个用于实现 IRouteConstraint  接口且包含 Match  方法的对象
。通过使用正则表达式可以规定参数格式,比如controller参数只能
4位数字: new { controller = @"\d{4}"}


routes.MapRoute("酒店列表页""hotels/{action}-{city}-{price}-{star}"new           {               controller = "Hotel",               action = "list",               city = "beijing",               price = "-1,-1",               star = "-1"           }, new           {               city = @"[a-zA-Z]*",               price = @"(\d)+\,(\d)+",               star = "[-1-5]"           });            routes.MapRoute("酒店首页""hotels/{*iiii}"new           {               controller =                   "Hotel",               action = "default",               hotelid = ""           });            routes.MapRoute("网站首页""{*values}"new           {               controller =                   "Home",               action = "index"           });


0 0
原创粉丝点击