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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值