Set URL in HttpContext

Niladri.Biswas
Posted by Niladri.Biswas under ASP.NET category on | Points: 40 | Views : 3896
Write the below function

public HttpContext SetNewHttpContext(string uRL)
{
var httpRequest = new HttpRequest("", uRL, "");
var httpResponse = new HttpResponse(new StringWriter());
return new HttpContext(httpRequest, httpResponse);
}


Invoke it as under

var oldURL = HttpContext.Current.Request.Url;
HttpContext.Current = SetNewHttpContext("http://root/test.aspx?userid=319279549&name=xyz");
var newURL = HttpContext.Current.Request.Url;


N.B.~ If we want to read the query string value, we can do so by

var queryString = System.Web.HttpUtility.ParseQueryString(newURL.Query, Encoding.UTF8);
var valUserName = queryString["name"];


Hope this helps

Comments or Responses

Login to post response