0

I would like to better understand the issue of setting new parameters to a url and retrieve it via

var ParaValue = Request.QueryString["parameterName"];

so if I have a URL : "http://www.myWebsite.aspx?UserName=Alice"

I will retrieve it via example above

string uName = Request.QueryString["UserName"].ToString();

but what if I want to alter value e.g. make UserName = "Ralf"

  • Re Edit

when a button is pressed , there is a parameter "state" that holds a reference to wich button was pressed the value of state was = "none" now i want to set it to img_button1.

i am not even sending the actuall imgbutton id

i am hard coding it just for testing /referencing

so i could know i am in the stage of event reaquested by the procidure of the given event of button1

when event triggerd by img_button2

i whould then want to set the state to "img_button2" etc'

LoneXcoder
  • 2,121
  • 6
  • 38
  • 76

3 Answers3

3

after I have made my research (I couldn't mark any answer kindly given here on my post) then I tested two options I've encountered in this Stack Overflow Page:

first option (given by Ahmad Mageed) I have tested to work just fine . and readability was easy to understand (as I am still fresh to asp.net 'tricks')

then followed the answer by annakata which was remarkably improved approach in the way that you don't actually have to redirect to achieve result - Query String IS modified

after playing around i have desided to follow annakatas approach and make a helper method that was using also a redirerion option with modified QueryString Parameters & values.

public void QuerStrModify(string CurrQS_ParamName, string NewQs_paramName, string NewPar_Value, bool redirectWithNewQuerySettings = false)
{

    // reflect to readonly property 
    PropertyInfo isReadOnly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

    // make collection editable 
    isReadOnly.SetValue(this.Request.QueryString, false, null);

    // remove 
    this.Request.QueryString.Remove(CurrQS_ParamName);

    // modify 
    this.Request.QueryString.Set(NewQs_paramName, NewPar_Value);

    // make collection readonly again 
    isReadOnly.SetValue(this.Request.QueryString, true, null);
    string FullUrl = Request.Url.AbsolutePath;
    if (redirectWithNewQuerySettings)
    {
        Response.Redirect(string.Join("?", FullUrl, this.Request.QueryString));
    }

}

i find it very helpful to someone that has Considerably less experience with asp.net developmet so i posted it as my version of correct answer , as i see it . i hope it'll help somoeone else that seeks the same Solution.

feel free to further improve it , as i mentiond I'm not a proven talent ..yet.

Community
  • 1
  • 1
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
0

You can use HttpModule

public class SimpleRewriter : System.Web.IHttpModule
{

    HttpApplication _application = null;

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new System.EventHandler(context_BeginRequest);
        _application = context;
    }

    public void Dispose()
    {
    }

    private void context_BeginRequest(object sender, System.EventArgs e)
    {
        string requesturl =
            _application.Context.Request.Path.Substring(0,
                _application.Context.Request.Path.LastIndexOf("//")
            );

        string[] parameters = requesturl.Split(new char[] { '/' });

        if (parameters.Length > 1)
        {
            string firstname = parameters[1];
            string lastname = parameters[2];


            //Here you can modify your parameters or your url

            _application.Context.RewritePath("~/unfriendly.aspx?firstname=" +
                firstname + "&lastname=" + lastname);

        }
    }
}

Link : http://msdn.microsoft.com/en-us/library/ms972974.aspx

Registering :

<configuration>
  <system.web>
    <httpModules>
      <add name="SimpleRewriter" type="SimpleRewriter"/>
     </httpModules>
  </system.web>
</configuration>

Link : http://msdn.microsoft.com/en-us/library/ms227673%28v=vs.100%29.aspx

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • that looks heavy , i guess nothing about that is too simple . thanks for all replys i will now look into it ! – LoneXcoder Sep 22 '12 at 15:23
  • It's easy just add this class to your solution and register your module in web.config, run application and set breakpoint for analyse – Aghilas Yakoub Sep 22 '12 at 15:27
  • easy easy my friend . i can understand new class inside the project as opposed to new item - new web form . breakPoints are like my heart pulses i use them all times, the"Module Part" is a chinese term for me though, and i staretd off via inside my form it does not matter but what to supply as `HttpApplication context` – LoneXcoder Sep 22 '12 at 15:47
  • @Lone i'am happy that you solved your problem my friend, you can create class in same project , you can create class in another library , type of item is class – Aghilas Yakoub Sep 22 '12 at 15:49
  • is it too basic the "Module" thing? as with web-form web.config add a class ? am i lacking a basic element ? (also HttpApplication context) , i am 5 month into development in .net Winforms and over two month trying asp.net – LoneXcoder Sep 22 '12 at 15:54
  • Ok Lone test and tell me your report – Aghilas Yakoub Sep 22 '12 at 16:12
  • i need to move that to chat . i hope you dont mind, Error 33 Extension method must be defined in a non-generic static class - this is an error after edding the module , refereing to one of my pages code behind main page declaration public partial class WorkDaysPerMonth : System.Web.UI.Page { – LoneXcoder Sep 22 '12 at 16:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17000/discussion-between-lonexcoder-and-aghilas-yakoub) – LoneXcoder Sep 22 '12 at 16:28
0

The problem here is a "source of truth" problem. The NameValueCollection exposed by HttpRequest.QueryString exposes the query string and should not be modified because the query string was supplied by the caller. If the application has a UserName query string argument but it may need to be changed, such as for testing, wrap it in a method that can get it form an alternate source if needed. For example:

// A very simple container
public static class SystemInfo
{
    // This would be an instance of QueryStringUserInfo
    // by default but could be changed for testing.
    public IUserInfo UserInfo
    {
        get;
        private set;
    }
}

// An interface that contains the user operations 
public interface IUserInfo
{
    string UserName { get; }
}

// Get the user name from the query string. This would
// be the default.
public class QueryStringUserInfo: IUserInfo
{
    public string UserName
    {
        get
        {
            return Request.QueryString["UserName"].ToString();
        }
    }
}

// Get the user name from the query string. This would
// be the default.
public class TestUserInfo: IUserInfo
{
    public string UserName
    {
        get
        {
            return "Foo";
        }
    }
}

So, rather than call the QueryString directly for the user name (or whichever piece of information), call the property on SystemInfo (horrible name but you get the point). It allows the source of the settings to be changed, such as if the code is used outside a web page or for testing.

akton
  • 14,148
  • 3
  • 43
  • 47
  • i was giving it as an example just to simplify ,say if i am not really accessing a parameter of a user name . the truth is i was trying to test and show values and make decisions based on the value that was altered via event or condition. ill try to refine my question... – LoneXcoder Sep 22 '12 at 15:10
  • " wrap it in a method " - part , could you give a simpler example that will be easier to implement ? i could use the class suggested by @Yakoub but it looks very complex at least for me as a newb , is there any chance to simplify that ? – LoneXcoder Sep 22 '12 at 15:32
  • @LoneXcoder I have added a simple example demonstrating the concept. – akton Sep 22 '12 at 23:54