检索ActionExecutingContext的实例

时间:2015-04-28 07:00:04

标签: c# asp.net-mvc

我想在

中检索ActionExecutingContext的实例
  

public ActionResult Contact2(string one,string two)

而不是在班类albumAttribute。

有可能吗?

谢谢!enter image description here

        [HttpPost]
        [album]
        public ActionResult Contact2(string one, string two)
        {
            ViewBag.Message = "Your contact page.";

            var ss = Response.Status;

            var genres = new List<Genre>
            {
                new Genre { Name = "Disco"},
                new Genre { Name = "Jazz"},
                new Genre { Name = "Rock"}
            };
            //return View(genres);


            //return View("contact2", genres);

            return View("contact22", genres);
        }





    public class albumAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            HttpRequestBase req = filterContext.HttpContext.Request;
            HttpResponseBase res = filterContext.HttpContext.Response;

             UriBuilder uriBuilder = new UriBuilder("http://" + req.Url.Authority + req.Url.LocalPath);
             NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query);
             query.Add("album", "first");
             uriBuilder.Query = query.ToString();

             string url = req.Url.AbsolutePath.ToString();
             res.Redirect(uriBuilder.Uri.OriginalString);

             base.OnActionExecuting(filterContext);

             /*
                UriBuilder uriBuilder = new UriBuilder("http://" + req.Url.Authority + "/Home/About");
                res.Redirect(uriBuilder.Uri.OriginalString);

                base.OnActionExecuting(filterContext);
            */     

        }
}

1 个答案:

答案 0 :(得分:1)

根据您的意见:

操作过滤器在操作之前执行,因此在操作中您将无法使用base.OnActionExecuting(filterContext)

除了可以在没有ActionExecutingContext对象的情况下执行图像中附加的所有代码之外,只需将其添加到Action中,并使用Response和{{1}获取请求和响应对象控制器属性。 您也可以使用

Request

而不是return this.Redirect(yourUrl);

res.Redirect(...)