1

I have an external application (written from a different company) which fires some requests to my asp.net web application when hitting a button. My application is running on https://localhost:59917/Main.aspx. If I hit the request button in the external program it says the following:

08:53:25 Requesting web page : https://localhost:59917/Main.aspx?token=mcQYVqoe7LxmBx7jgHBq6FtXXp4uPCzX0FDZStiZDFMDd4l6oB3x5CgysXJKgy2y

So this app is now requesting my web application. I now have to get the given arguments of this request (in this example I need to read the argument token). I found this thread and tried the following in my code behind (Main.aspx.cs):

protected void btnListener_Click(object sender, EventArgs e)
{
    string token = Request.QueryString["token"];
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertToken", "alert('" + token + "');", true);
}

But this returns an empty string. Since I'm new to asp.net and web development I'm not sure if I understand the HttpRequest class correctly. Is this even the right way to get the arguments I need?

4
  • 1
    The code above should be moved presumably in the main.aspx.cs file under the Page_Load method when IsPostBack == false. Commented May 22, 2017 at 8:47
  • Why in Page_Load() method? This method is only fired on startup, right? But the request could be fired anytime. Commented May 22, 2017 at 8:51
  • Page_Load is fired everytime a request arrives to the page. So, if the external application makes a request to main.aspx passing the mentioned querystring you should receive a Page_Load event in your main.aspx.cs Commented May 22, 2017 at 8:58
  • Oh, okay. I tried it and it works, I now have the requested token. I'll keep this in reminder. If you post this as an answer I'll accept it. Thank you very much :) Commented May 22, 2017 at 9:00

1 Answer 1

2

If the external application makes a request to the main.aspx page then a Page_Load events occurs in the Page Lifecycle

Thus you can simply move your code in the Page_Load event handler already written for you by the designer

protected void main_Load(object sender, EventArgs e)
{
    string token = Request.QueryString["token"];
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertToken", "alert('" + token + "');", true);
}

Actually Page_Load events are called everytime a request for the page comes trough the ASP.NET engine also when you click a button on the same page.

Before the call to the event handler for the button clicked you receive a call to the Page_Load event handler. You can discover if this is the first time that your page loads looking at the IsPostBack property (true when the load is the result of an action performed on the same page, false when the page is loaded to present its interface to the end-user)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.