Action的调用与属性
1、唤起Action
Route找到Action
唤起Action
-ControllerActionInvoker
1)找到对应的Action
2)找到当前请求发生的参数,匹配
3)调用Action方法所有的Filters
4)调用ExcuteResult
2、Action匹配到URL
从URL中匹配Action名称
ContextResult
定位?
3、Action选择
反射方式找到类,找到方法
必须为Public
标准:
-不允许有NonActionAttribute标记
-构造函数,属性控制器,事件访问器不能作为制定的Action方法
-继承自object的方法或继承自controller的方法
4、ActionNameAttribute
异步调用
指定别名 ,匹配标记的名字
[ActionName("View")]
public ActionResult ViewSomething(string id)
{
return View();
}
指定别名,
调用时 ActionName是View
系统关键字可以被别名重载
5、ActionSelectorAttribute
public abstract class ActionSelectorAttribute:Attribute
{
public abstract bool
IsValidForRequest(ControllerContext controllerContext,MethodInfo methodInfo);
}
实现两个接口
AcceptVerbsAttribute
[HttpGet]
public ActionResultEdit(string id)
{
return View();
}
[HttpPost]
public ActionResultEdit(string id, FormCollection form)
{
//Save the item and redirect…
}
6、模拟Rest请求
HttpPostAttribute c
HttpPutAttribute u
HttpGetAttribute r
HttpDeleteAttribute d
crud create read update delete
映射参数
/simple2/distance/0,0/1,2
来自以下三方面内容:
Request Form collection post
Route Data URL位置拆分
Request QueryString collection 查询字符串集合
Route Data 比较好
参数255个是极限
7、调用Action
使用异步Action
异步Controller
同步与异步的比较
使用同步方式
1.操作短小迅捷
2.要求高可测试性
3.这个操作要求高CPU而不是高IO
使用异步操作
1.通过测试发现该操作是网站应用性能瓶颈
2.对并行性有高要去
3.这个操作要求高IO而不是高cpu
Output Cache解决异步 并行性
例子
使用同步方式:
public class PortalController: Controller
{
public ActionResultNews(string city)
{
NewsServicenewsService= new NewsService();
NewsModelnews = newsService.GetNews(city);
return View(news);
}
}
使用异步
public class PortalController: AsyncController{
public void NewsAsync(string city) { //函数名称有指定
AsyncManager.OutstandingOperations.Increment();//整理MVC当前线程
NewsServicenewsService= new NewsService();
newsService.GetNewsCompleted+= (sender, e) => {
AsyncManager.Parameters[“news”] = e.News;
AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetNewsAsync(city);
}
public ActionResultNewsCompleted(NewsModelnews) { //多了一指定方法,名称也有指定
return View(news);
}
}
这两方法是特定的,不能写为Action名字
要用可以用AttributeName指定
8、并行操作的性能
同步
public class PortalController: Controller {
public ActionResultIndex(string city) {
NewsServicenewsService= new NewsService();
NewsModelnewsModel= newsService.GetNews(city);
WeatherServiceweatherService= new WeatherService();
WeatherModelweatherModel= weatherService.GetWeather(city);
SportsServicesportsService= new SportsService();
SportsModelsportsModel= sportsService.GetScores(city);
PortalViewModelmodel = new PortalViewModel{
News = newsModel,Weather = weatherModel,
Sports = sportsModel
};
return View(model);
}
异步
public class PortalController: AsyncController{
public void IndexAsync(string city) {
AsyncManager.OutstandingOperations.Increment(3);
NewsService newsService= new NewsService();
newsService.GetNewsCompleted+= (sender, e) => {
AsyncManager.Parameters[“news”] = e.News;
AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetNewsAsync(city);
WeatherService weatherService= new WeatherService();
weatherService.GetWeatherCompleted+= (sender, e) => {
AsyncManager.Parameters[“weather”] = e.Weather;
AsyncManager.OutstandingOperations.Decrement();
};
weatherService.GetWeatherAsync(city);
SportsService sportsService= new SportsService();
sportsService.GetScoresCompleted+= (sender, e) => {
AsyncManager.Parameters[“sports”] = e.Scores;
AsyncManager.OutstandingOperations.Decrement();
};
SportsModel sportsModel= sportsService.GetScoresAsync(city);
}
public ActionResultIndexCompleted(NewsModelnews,WeatherModelweather, SportsModelsports) {
PortalViewModel model = new PortalViewModel{
News = news,Weather= weather,Sports= sports
};
return View(model);
}
}
三个时间是并行的,是最大时间不是三个时间的总和。
9、对异步请求使用标签
[Authorize]
public void ActionAsync(){
//...
}
[Authorize]//这里晚了,所以错误
public ActionResult ActionCompleted(){
//..
}
超时验证
[HandleError(ExceptionType=typeof(TimeoutException))]
[AsyncTimeout]
[NoAsyncTimeout]//不限制超时 最好不要用
[AsyncTimeout(60000)]//45秒
public void ActionAsync(){
//...
}
[NoAsyncTimeout]
pulic class PortalController:AsyncController{
//....
}
两种使用方法
附加说明:
[ActionName(“ReservationCompleted”)]
public ActionResultSomeOtherName() {}
BeginMethod()/EndMethod()
public void NewsAsync(string city) {
AsyncManager.OutstandingOperations.Increment();
NewsService newsService= new NewsService();
newsService.BeginGetNews(city, ar=> {AsyncManager.Sync(() => {
AsyncManager.Parameters[“news”] =newsService.EndGetNews(ar);
AsyncManager.OutstandingOperations.Decrement();});}, null);}
方法是异步,但内部是同步
10、更新Model层UpdateModel
[HttpPost]
public ActionResultEdit(Product product)
{
if(ModelState.IsValid){
//simulate save to the DB
db.SaveChanges(product);
ViewData[“Message”] = product.ProductName+ “ Updated”;
return RedirectToAction(“Edit”);
}
else
{
return View(product);
}}
post过来,RedirectToAction
不会重复刷新重复提交。
11、验证数据
public class Product {
[Required(ErrorMessage= “The product name must not be empty.”)]
public string ProductName{ get; set; }
[Range(0, double.MaxValue,
ErrorMessage=”The unit price must be larger than 0.00.”)]
public double UnitPrice{ get; set; }
}
Model层
12、安全性
永远不要不加处理的使用用户的输入
2011-4-19 23:10 danny
1、唤起Action
Route找到Action
唤起Action
-ControllerActionInvoker
1)找到对应的Action
2)找到当前请求发生的参数,匹配
3)调用Action方法所有的Filters
4)调用ExcuteResult
2、Action匹配到URL
从URL中匹配Action名称
ContextResult
定位?
3、Action选择
反射方式找到类,找到方法
必须为Public
标准:
-不允许有NonActionAttribute标记
-构造函数,属性控制器,事件访问器不能作为制定的Action方法
-继承自object的方法或继承自controller的方法
4、ActionNameAttribute
异步调用
指定别名 ,匹配标记的名字
[ActionName("View")]
public ActionResult ViewSomething(string id)
{
return View();
}
指定别名,
调用时 ActionName是View
系统关键字可以被别名重载
5、ActionSelectorAttribute
public abstract class ActionSelectorAttribute:Attribute
{
public abstract bool
IsValidForRequest(ControllerContext controllerContext,MethodInfo methodInfo);
}
实现两个接口
AcceptVerbsAttribute
[HttpGet]
public ActionResultEdit(string id)
{
return View();
}
[HttpPost]
public ActionResultEdit(string id, FormCollection form)
{
//Save the item and redirect…
}
6、模拟Rest请求
HttpPostAttribute c
HttpPutAttribute u
HttpGetAttribute r
HttpDeleteAttribute d
crud create read update delete
映射参数
/simple2/distance/0,0/1,2
来自以下三方面内容:
Request Form collection post
Route Data URL位置拆分
Request QueryString collection 查询字符串集合
Route Data 比较好
参数255个是极限
7、调用Action
使用异步Action
异步Controller
同步与异步的比较
使用同步方式
1.操作短小迅捷
2.要求高可测试性
3.这个操作要求高CPU而不是高IO
使用异步操作
1.通过测试发现该操作是网站应用性能瓶颈
2.对并行性有高要去
3.这个操作要求高IO而不是高cpu
Output Cache解决异步 并行性
例子
使用同步方式:
public class PortalController: Controller
{
public ActionResultNews(string city)
{
NewsServicenewsService= new NewsService();
NewsModelnews = newsService.GetNews(city);
return View(news);
}
}
使用异步
public class PortalController: AsyncController{
public void NewsAsync(string city) { //函数名称有指定
AsyncManager.OutstandingOperations.Increment();//整理MVC当前线程
NewsServicenewsService= new NewsService();
newsService.GetNewsCompleted+= (sender, e) => {
AsyncManager.Parameters[“news”] = e.News;
AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetNewsAsync(city);
}
public ActionResultNewsCompleted(NewsModelnews) { //多了一指定方法,名称也有指定
return View(news);
}
}
这两方法是特定的,不能写为Action名字
要用可以用AttributeName指定
8、并行操作的性能
同步
public class PortalController: Controller {
public ActionResultIndex(string city) {
NewsServicenewsService= new NewsService();
NewsModelnewsModel= newsService.GetNews(city);
WeatherServiceweatherService= new WeatherService();
WeatherModelweatherModel= weatherService.GetWeather(city);
SportsServicesportsService= new SportsService();
SportsModelsportsModel= sportsService.GetScores(city);
PortalViewModelmodel = new PortalViewModel{
News = newsModel,Weather = weatherModel,
Sports = sportsModel
};
return View(model);
}
异步
public class PortalController: AsyncController{
public void IndexAsync(string city) {
AsyncManager.OutstandingOperations.Increment(3);
NewsService newsService= new NewsService();
newsService.GetNewsCompleted+= (sender, e) => {
AsyncManager.Parameters[“news”] = e.News;
AsyncManager.OutstandingOperations.Decrement();
};
newsService.GetNewsAsync(city);
WeatherService weatherService= new WeatherService();
weatherService.GetWeatherCompleted+= (sender, e) => {
AsyncManager.Parameters[“weather”] = e.Weather;
AsyncManager.OutstandingOperations.Decrement();
};
weatherService.GetWeatherAsync(city);
SportsService sportsService= new SportsService();
sportsService.GetScoresCompleted+= (sender, e) => {
AsyncManager.Parameters[“sports”] = e.Scores;
AsyncManager.OutstandingOperations.Decrement();
};
SportsModel sportsModel= sportsService.GetScoresAsync(city);
}
public ActionResultIndexCompleted(NewsModelnews,WeatherModelweather, SportsModelsports) {
PortalViewModel model = new PortalViewModel{
News = news,Weather= weather,Sports= sports
};
return View(model);
}
}
三个时间是并行的,是最大时间不是三个时间的总和。
9、对异步请求使用标签
[Authorize]
public void ActionAsync(){
//...
}
[Authorize]//这里晚了,所以错误
public ActionResult ActionCompleted(){
//..
}
超时验证
[HandleError(ExceptionType=typeof(TimeoutException))]
[AsyncTimeout]
[NoAsyncTimeout]//不限制超时 最好不要用
[AsyncTimeout(60000)]//45秒
public void ActionAsync(){
//...
}
[NoAsyncTimeout]
pulic class PortalController:AsyncController{
//....
}
两种使用方法
附加说明:
[ActionName(“ReservationCompleted”)]
public ActionResultSomeOtherName() {}
BeginMethod()/EndMethod()
public void NewsAsync(string city) {
AsyncManager.OutstandingOperations.Increment();
NewsService newsService= new NewsService();
newsService.BeginGetNews(city, ar=> {AsyncManager.Sync(() => {
AsyncManager.Parameters[“news”] =newsService.EndGetNews(ar);
AsyncManager.OutstandingOperations.Decrement();});}, null);}
方法是异步,但内部是同步
10、更新Model层UpdateModel
[HttpPost]
public ActionResultEdit(Product product)
{
if(ModelState.IsValid){
//simulate save to the DB
db.SaveChanges(product);
ViewData[“Message”] = product.ProductName+ “ Updated”;
return RedirectToAction(“Edit”);
}
else
{
return View(product);
}}
post过来,RedirectToAction
不会重复刷新重复提交。
11、验证数据
public class Product {
[Required(ErrorMessage= “The product name must not be empty.”)]
public string ProductName{ get; set; }
[Range(0, double.MaxValue,
ErrorMessage=”The unit price must be larger than 0.00.”)]
public double UnitPrice{ get; set; }
}
Model层
12、安全性
永远不要不加处理的使用用户的输入
2011-4-19 23:10 danny
发表评论
-
10.2Asp.net MVC各层使用TDD方式
2011-05-01 12:09 769Asp.net MVC各层使用TDD方式 Asp.net M ... -
10.1TDD简介
2011-04-30 23:09 533MVC中的测试驱动开发 为什么需要TDD TDD的由来 它 ... -
9、MVC程序安全限定
2011-04-29 22:14 1785常见网络安全攻击隐患 针对Asp.net MVC防御体系 安 ... -
8.2ASP.NET MVC2使用Ajax类库实例
2011-04-29 13:05 894ASP.NET MVC2使用Ajax类库实例 .net fo ... -
8.1Ajax类库介绍
2011-04-28 09:12 855在ASP.NET MVC中使用 -Microsoft ASP. ... -
7.2自定义开发Filter
2011-04-27 16:04 596Custom Filters ➤IAuthorization ... -
7.1Filter的使用
2011-04-26 22:20 475Filter的使用 -对Action的附加说明 Asp.n ... -
6.2ActionResult的使用
2011-04-25 19:14 652ActionResult public abstract c ... -
6.1Controller类的基本构成
2011-04-25 09:17 641关于Controller Controller是什么? C ... -
5.6在Webform中使用routing
2011-04-23 22:48 632在Webform中使用routing 在Asp.net 4中使 ... -
5.5URL和Action匹配的秘密
2011-04-22 09:11 868Routes是如何把URL映射为Action -请求路由管道 ... -
5.4使用routing生成URL
2011-04-21 11:05 566使用routing生成URL URL生成器概述 1、质询每个 ... -
5.3使用Routes
2011-04-20 09:11 7155.3使用Routes 注册Area Route publi ... -
5.2Routes匹配URL的工作方式
2011-04-19 08:55 825URL的匹配规则 -site/{controller}/{ac ... -
5.1Url和Routes介绍
2011-04-17 12:05 6215.1Url和Routes介绍 关于Urls -域名好记好拼 ... -
4、View引擎介绍
2011-04-16 22:03 616View引擎介绍 Request =>Routing= ... -
3、HtmlHelper类(续)
2011-04-16 13:41 621HtmlHelper类(续) Html.TextArea 用 ... -
2、HtmlHelper类
2011-04-15 22:33 1070HtmlHelper类 <%Html. MVC1 & ... -
1、View层
2011-04-15 09:06 601ViewDataDictionary类 View 负责输出 ...
相关推荐
6.3 使用Hessian和Burlap的远程调用 6.3.1 访问Hessian/Burlap服务 6.3.2 用Hessian或Burlap公开Bean的功能 6.4 使用HTTP invoker 6.4.1 通过HTTP访问服务 6.4.2 把Bean作为HTTP服务公开 ...
6.3 数据标签 117 6.3.1 property标签 117 6.3.2 set标签 118 6.3.3 push标签 119 6.3.4 bean标签 120 6.3.5 action标签 122 6.4 控制标签 124 6.4.1 iterator标签 124 6.4.2 if和else标签 125 6.5 其他标签 126 ...
6.3在spring中编写事务 6.4声明式事务 6.4.1定义事务参数 6.4.2代理事务 6.4.3在spring2.0里声明事务 6.4.4定义注释驱动事务 6.5小结 第7章保护spring 7.1springsecurity介绍 7.2验证用户身份 7.2.1配置...
6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...
6.3 在Spring中编写事务 6.4 声明式事务 6.4.1 定义事务参数 6.4.2 代理事务 6.4.3 在Spring 2.0里声明事务 6.4.4 定义注释驱动事务 6.5 小结 第7章 保护Spring 7.1 Spring Security介绍 7.2 验证用户身份...
6.3 在Spring中编写事务 6.4 声明式事务 6.4.1 定义事务参数 6.4.2 代理事务 6.4.3 在Spring 2.0里声明事务 6.4.4 定义注释驱动事务 6.5 小结 第7章 保护Spring 7.1 Spring Security介绍 7.2 验证用户身份...
6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...
6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...
6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...
6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与...
6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——...
6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...
6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...
6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...
6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...
14.1 新增表单元素与属性 256 14.1.1 新增的属性 256 14.1.2 增加与改良的input元素的种类 259 14.1.3 output元素的添加 264 14.1.4 应用新增元素制作注册表单 264 14.2 对表单的验证 266 14.2.1 自动...
Action、Category属性与intent-filter配置; Data、Type属性与intent-filter配置; Extra属性; 5.3、使用Intent创建Tab页面: 第6章、Android应用的资源 6.1、资源的类型及存储方式: 6.2、使用字符串、颜色、...