GibboK
GibboK

Reputation: 73898

How to add a query string to a URL using ActionFilterAttribute?

Using asp.net MVC 3, I would like to add a query string on every request of Controller's method, using the following code, the query string is not being added to the URL.

Any idea what I am doing wrong or any other alternative approaches?

  public ActionResult Index(string uniquestudentreference)
    {
        return View(eventListVM);
    }

 public class UserAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.ActionParameters["uniquestudentreference"] = Various.GetGivenNameUser();
        }
    }

Upvotes: 0

Views: 414

Answers (2)

Haney
Haney

Reputation: 34742

What you want to do here is set the value to the ViewData or TempData of the ControllerContext in the ActionResult, and then reference it in the method. This is a better way to pass it around than querystring, and safer too (avoids user being able to manipulate the value). That or store the value in the Session.

Upvotes: 0

Dietz
Dietz

Reputation: 578

If you want the uniquestudentreference parameter to have a value in your controller action, you should simply invoke the controller action with the correct url like:

http://www.someurl.com/<controller>/Index/<uniquestudentreference>

If you want to affect how you pass information through the url, you should look into customizing your route table (located in Global.asax)

You can read more about that here: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

Upvotes: 1

Related Questions