【问题标题】:altering QueryString parameters / values更改 QueryString 参数/值
【发布时间】:2012-09-22 15:00:40
【问题描述】:

我想更好地理解为 url 设置新参数的问题 并通过

检索它

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

如果我有一个 URL:“http://www.myWebsite.aspx?UserName=Alice”

我将通过上面的示例检索它

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

但是如果我想改变价值呢?使 UserName = "Ralf"

  • 重新编辑

按下按钮时, 有一个参数“state”包含对按下的按钮的引用 state 的值为 = "none" 现在我想将它设置为 img_button1。

我什至没有发送实际的 imgbutton id

我只是为了测试/引用而对其进行硬编码

所以我可以知道我处于给定事件的过程所要求的事件阶段 按钮1

img_button2触发事件时

然后我想将状态设置为“img_button2” 等等'

【问题讨论】:

    标签: asp.net c#-4.0 request query-string asp.net-4.0


    【解决方案1】:

    在我进行研究之后(我无法在我的帖子中标记任何友好给出的答案) 然后我测试了我在this Stack Overflow Page 中遇到的两个选项:

    第一个选项(由 Ahmad Mageed 给出)我已经测试过可以正常工作。 并且可读性很容易理解(因为我对 asp.net 'tricks'仍然很陌生)

    然后按照答案 anakata 以您的方式显着改进了方法 实际上不必重定向即可获得结果 - 查询字符串已修改

    在玩了之后,我决定遵循annakatas 的方法 并制作一个使用 redirerion 选项的辅助方法 带有修改的 QueryString 参数和值。

    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));
        }
    
    }
    

    我发现它对那些在 asp.net 开发方面经验相当少的人很有帮助 所以我把它发布为我的正确答案版本,正如我所见。 我希望它能帮助寻求相同解决方案的其他人。

    请随意进一步改进它,正如我提到的那样,我还不是一个成熟的人才。

    【讨论】:

      【解决方案2】:

      你可以使用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);
      
              }
          }
      }
      

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

      注册:

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

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

      【讨论】:

      • 看起来很重,我想这没什么太简单的。感谢所有回复,我现在会调查它!
      • 很简单,只需将此类添加到您的解决方案中并在 web.config 中注册您的模块,运行应用程序并设置断点进行分析
      • 简单易行,我的朋友。我可以理解项目中的新类,而不是新项目 - 新 Web 表单。断点就像我的心跳一样,我一直在使用它们,“模块部分”对我来说是一个中文术语,我通过在我的表格中开始关注它并不重要,但提供什么作为HttpApplication context
      • @Lone 我很高兴你解决了你的问题我的朋友,你可以在同一个项目中创建类,你可以在另一个库中创建类,项目类型是类
      • “模块”这个东西太基本了吗?与 web-form web.config 一样添加一个类?我缺乏基本要素吗? (也是 HttpApplication 上下文),我在 .net Winforms 中进行了 5 个月的开发,并且尝试了两个多月的 asp.net
      【解决方案3】:

      这里的问题是“真相来源”问题。 HttpRequest.QueryString 公开的 NameValueCollection 公开了查询字符串,不应修改,因为查询字符串是由调用者提供的。如果应用程序有一个 UserName 查询字符串参数,但它可能需要更改(例如为了测试),请将其包装在一个方法中,以便在需要时将其从备用源中获取。例如:

      // 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";
              }
          }
      }
      

      因此,与其直接为用户名(或任何一条信息)调用 QueryString,不如调用 SystemInfo 上的属性(可怕的名称,但你明白了)。它允许更改设置的来源,例如代码是否用于网页之外或用于测试。

      【讨论】:

      • 我只是为了简化而举个例子,比如如果我没有真正访问用户名的参数。事实是我试图测试和展示价值,并根据通过事件或条件改变的价值做出决定。生病尝试完善我的问题...
      • "将其包装在一个方法中"-部分,你能举一个更简单的例子吗?我可以使用@Yakoub 建议的课程,但至少对于我作为新手来说,它看起来非常复杂,有没有机会简化它?
      • @LoneXcoder 我添加了一个简单的例子来展示这个概念。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多