2012-09-22 168 views
0

我想更好地了解新的参数设置为URL 的问题,并通过改变查询字符串参数/值

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

所以检索它,如果我有一个URL:“HTTP:// WWW .myWebsite.aspx?用户名=爱丽丝”

我将通过例如检索它上面

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

但是如果我想改变数值例如使用户名=“拉尔夫”

  • 重新编辑

当按钮被按下时, 有一个参数的“状态”,其保持参考至极按钮被按下 状态的值= “none” 现在我想将其设置为img_button1。

我甚至不发送实际工作imgbutton ID

我努力的编码它只是用于测试/参考

,所以我可以知道我在事件中由给定事件的procidure reaquested阶段的 BUTTON1

img_button2

事件triggerd我然后对子级要设置的状态为 “img_button2” 等”

回答

3

我做我的研究(我不能标志着这里恳请给我交任何回答)后 然后我测试我在this Stack Overflow Page遇到了两个选项:

第一个选项(艾哈迈德Mageed给出)我已经测试过,工作得很好。 和可读性容易理解(因为我仍然记忆犹新到asp.net“招数”)

再其次 annakata答案这是显着改善的方式方法,你 实际上并不重定向实现结果 - 查询字符串玩弄我已经desided遵循annakata的做法 并作出使用也redirerion选项 与修改查询字符串参数&值的辅助方法,修改后

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意识中 所以我张贴我的版本正确答案的,因为我看到了相当少的经验非常有帮助。 我希望它能帮助其他人寻求相同的解决方案。

随时可以进一步改善它,因为我提到我不是一个成熟的人才..yet。

0

您可以使用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

+0

看起来很沉重,我猜想这件事太简单了。感谢所有回复,我现在将对它进行调查! – LoneXcoder

+0

只需将此类添加到您的解决方案,并在web.config中注册您的模块,运行应用程序并设置断点以便分析 –

+0

轻松容易我的朋友,这很容易。我可以理解项目内部的新课程,而不是新的项目 - 新的Web表单。 breakPoints就像我的心脏脉冲我一直都在使用它,但是“模块部分”对我来说是一个中文术语,而且我通过内部表单来关闭它并不重要,但是作为'HttpApplication context'提供的内容' – LoneXcoder

0

这里的问题是 “真理的来源” 的问题。 HttpRequest.QueryString公开的NameValueCollection公开查询字符串,因此不应修改,因为查询字符串是由调用者提供的。如果应用程序具有用户名查询字符串参数,但可能需要更改(例如进行测试),则可以将其包装在一个方法中,以便根据需要将其替换为备用来源。例如:

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

因此,而不是直接调用查询字符串为用户名(或任何资料片),呼吁系统的系统信息的属性(可怕的名字,但你明白了吧)。它允许更改设置的来源,例如代码是在网页外部使用还是用于测试。

+0

我给它作为一个简单的例子,如果我没有真正访问一个用户名的参数。事实是,我试图测试并显示价值,并根据通过事件或条件改变的价值做出决定。生病尝试改进我的问题... – LoneXcoder

+0

“将其包裹在一个方法中” - 部分,你可以给出一个更简单的例子,更容易实现吗?我可以使用@Yakoub建议的类,但它看起来非常复杂,至少对于我来说,作为一个新手,有没有可能简化它呢? – LoneXcoder

+0

@LoneXcoder我已经添加了一个简单的例子来演示这个概念。 – akton