RPM*_*984 13 asp.net-mvc asp.net-mvc-routing action-filter actionfilterattribute asp.net-mvc-4
给出以下代码:
public class MyActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var a = filterContext.ActionParameters["someKey"];
var b = filterContext.RouteData.Values["someKey"];
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
a和之间有什么区别b?
什么时候我们应该使用行动参数而不是路线数据?有什么不同?
Fel*_*ani 17
当您使用ActionParameterson时OnActionExecuting,您可以在处理操作之前更改客户端发送的值,以获取示例:
public class MyActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.ActionParameters["customerId"] = 852;
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您有使用customerId参数的操作,您将获得在操作过滤器上设置的值,因为您的操作具有过滤器,用于示例:
当您请求任何这样的网址时/customer/detail/123,您将获得852CustomerId的价值:
[MyAction]
public ActionResult Detail(int customerId)
{
// customerId is 852
return View();
}
Run Code Online (Sandbox Code Playgroud)
RouteData 只是值在URL上,由路由表处理.
| 归档时间: |
|
| 查看次数: |
11097 次 |
| 最近记录: |