`

6.3Action的调用与属性

阅读更多
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
分享到:
评论

相关推荐

    spring in action英文版

     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服务公开  ...

    Struts2 in action中文版

    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 ...

    Spring in Action(第2版)中文版

    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配置...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...

    Spring in Action(第二版 中文高清版).part2

    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 验证用户身份...

    Spring in Action(第二版 中文高清版).part1

    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 验证用户身份...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...

    Google Android SDK开发范例大全的目录

    6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...

    Google+Android+SDK开发范例大全

    6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少——使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED 6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——...

    Google Android sdk 开发范例大全 部分章节代码

    6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...

    Google Android SDK开发范例大全(完整版)

    6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...

    Google Android SDK 开发范例大全01

    6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...

    Google Android SDK 开发范例大全02

    6.3 群发拜年短信给联系人——ACTION_PICK与Uri对象 6.4 开始与停止系统服务——Service与Runnable整合并用 6.5 通过短信发送email通知——BroadcastReceiver与Intent整合 6.6 手机拨接状态——PhoneStateListener之...

    从入门到精通HTML5——PDF——网盘链接

     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 自动...

    Android实例代码

    Action、Category属性与intent-filter配置; Data、Type属性与intent-filter配置; Extra属性; 5.3、使用Intent创建Tab页面: 第6章、Android应用的资源 6.1、资源的类型及存储方式: 6.2、使用字符串、颜色、...

Global site tag (gtag.js) - Google Analytics