【问题标题】:c# mvc get url parameter within an attributec# mvc获取属性内的url参数
【发布时间】:2011-07-15 22:31:26
【问题描述】:

在我的控制器中,我有这个验证 url 的“SomeAttribute”。它检查 url 中提供的 ID 是否为有效 ID,以及类别名称是否是给定 ID 的相关类别名称。

[SomeAttribute]
public ActionResult SomeAction() 
{
    ....
}

但是,在我的属性中,我不知道如何才能达到当前的 url 参数。

public SomeAttribute : ActionFilterAttribute {

   public override void OnActionExecuting(ActionExecutingContext filterContext) {
      string myurl = HttpContext.Current.Request["mystring"] // this does not work
   }
}

所以基本上我的问题是,我如何才能在属性中访问当前上下文的 url?

【问题讨论】:

    标签: c# url parameters attributes


    【解决方案1】:

    如果您在属性中需要 url 参数并且您正在使用路由,这就是解决方案:

    filterContext.RouteData.Values["mystring"]
    

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 2021-12-31
      • 2011-08-31
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多