1

Using MVC 3 Asp.Net, I would like to add a default Query String on any method in a controller.

Using the following code I get an error at line ... QueryString.Add():

Collection is read only.

Any idea how to fix it, or do you know a better way how to append a query string to method of a controller? Please post a sample of code thanks.

   public class HomeController : Controller
    {

        protected override void Initialize(RequestContext requestContext)
        {
            // Add the User's ID if is not present in the request
            string user = requestContext.HttpContext.Request.QueryString["UniqueStudentReference"];
            if (user == null)
            {

                string userId = Various.GetGivenNameUser();
                System.Web.HttpContext.Current.Request.QueryString.Add("UniqueStudentReference", userId);
            }

                base.Initialize(requestContext);
        }
...
amurra
  • 15,221
  • 4
  • 70
  • 87
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • could you please post a sample of code, I would be glad to accept an answer :-) – GibboK May 23 '13 at 11:31
  • What aim do you want to achieve? In general, you should not do this. – Kirill Bestemyanov May 23 '13 at 11:31
  • 2
    The querystring is part of the *request* that is handled by your *controller*. The request itself you cannot change. The real question is, what are you trying to accomplish? – Maarten May 23 '13 at 11:33
  • I need to pre append a query string for calls to methods of a controller. The query string should appear always. – GibboK May 23 '13 at 13:41
  • @GibboK please tell us what the problem is that you want to solve, because now you're already looking at a technical solution for a problem, but that solution doesn't sound like a proper MVC way of solving your problem – Hanno May 23 '13 at 13:53
  • Hi need to invalidate the Cache (OutputCache attributes) for URLs – GibboK May 23 '13 at 17:42

2 Answers2

1

What about redirecting?

    protected override void Initialize(RequestContext requestContext)
    {
        // Add the User's ID if is not present in the request
        string user = requestContext.HttpContext.Request.QueryString["UniqueStudentReference"];
        if (user == null)
        {

            string userId = Various.GetGivenNameUser();

            requestContext.HttpContext.Response.RedirectToRoute(new { UniqueStudentReference = userId });
        }

        base.Initialize(requestContext);
    }

This should redirect to the same route just adding a query string parameter 'UniqueStudentReference'

Carlos Martinez T
  • 6,458
  • 1
  • 34
  • 40
  • using this code I get this error The method or operation is not implemented on requestContext.HttpContext.Response.RedirectToRoute...... any idea? – GibboK May 23 '13 at 12:06
  • May be you will say us what do you want to achive with this? And we give you solution that will be in asp.net mvc paradigm. – Kirill Bestemyanov May 23 '13 at 12:35
  • I need to pre append a query string for calls to methods of a controller. The query string should appear always. – GibboK May 23 '13 at 13:14
  • Maybe it is because your are overriding the Initialize method instead of doing this in the Index action. If you needed on every action then use a FilterAttribute? – Carlos Martinez T May 23 '13 at 15:22
  • Why do you want to show this in query string? What is the meaning of this UniqueStudentReference value? Is it to determine what user requests or something else? – Kirill Bestemyanov May 24 '13 at 05:03
-1

It looks like you're trying to do something in 'webforms style', instead of 'MVC style'.

The default template for MVC is set up so that you can specify ID's in the URL, for example /Home/User/1 would give you ID=1. The 'webforms' URL would have been something like /users.aspx?id=1.

So my guess is that you just have to create an ActionMethod like

public ViewResult User(int id)
{
    return View(userRepository.Find(id)); // example where you're using EntityFramework
}

Actual name of the method may be something different offcourse. But the important thing is that the ID parameter will be set automatically by the MVC framework.

Hanno
  • 1,017
  • 11
  • 18