HelloWorld1
HelloWorld1

Reputation: 14108

Retrieve Instance of ActionExecutingContext

I would like to retrieve the instance of ActionExecutingContext inside of

public ActionResult Contact2(string one, string two)

and not in the class albumAttribute.

Is it possible to do it?

Thanks!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);
            */     

        }
}

Upvotes: 1

Views: 1854

Answers (1)

Alex Art.
Alex Art.

Reputation: 8781

Based on your comments:

Action filters execute prior to Actions so inside an Action you won't be able to use base.OnActionExecuting(filterContext).

Other than that all the code that's attached in the image could be executed without ActionExecutingContext object, just add it to your Action and for getting a Request and Response objects use Response and Request controller properties. You can also use

return this.Redirect(yourUrl);

instead of res.Redirect(...)

    [HttpPost]
    [album]
    public ActionResult Contact2(string one, string two)
    {
        var req = this.Request;
        var res = this.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();
        return this.Redirect(uriBuilder.Uri.OriginalString);            
    }

Upvotes: 1

Related Questions