Archive for May 8th, 2006

08 May 2006

Passing values between web applications

4 Comments ASP.NET

In my previous post, I have written about passing value between pages of same application. How about if you want to send some data from one application to another? In continuation of my previous post, let’s discuss about passing value between applications.

As you have guessed while reading title of the post, passing values through query string in most widely used methods of passing values.

WebApp1::FirstForm.aspx.cs
Response.Redirect("http://localhost/WebApp2/SecondForm.aspx?Parameter=" + TextBox1.Text);

WebApp2::SecondForm.aspx.cs
TextBox1.Text = Request. QueryString["Parameter"].ToString();

As I have discussed in my previous post, there are some constrains under which you can use this method.

The first one is that, you cannot pass value more than maximum allowed characters (ie. 2083 characters) as URL. If previous page passes value larger then that, your target application will crash as IIS can not handle URL of that size.

Second issue, as I have discussed before, values passed through query string are actually exposed to user in address bar of browser. So you must have to take care of validating these values after fetching from Request object in your target application. Yup, you can also encrypt the passed values to address this issue.

And third, you also have to take care of special characters in passed parameters.

My most preferred method to handle this kind of situation of passing value between two different applications is by posting form to another URL from previous application using javascript. Just like third methods of passing values in my previous post.

So first, we will create a web application with name “WebApp1”. It will have a web page “FirstForm.aspx” which will get the input from user and post the form to another page.

WebApp1::FirstForm.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
buttonPassValue.Attributes.Add("onclick", "return PostPage();");
}

And we create a javascript function to post the form.

WebApp1::FirstForm.aspx
<script language=javascript>
function PostPage()
{
document.Form1.action = "http://localhost/WebApp2/SecondForm.aspx";
document.Form1.method = "POST";
document.Form1.submit();
}
</script>

As you notice by explicit URL of target application, we are simply post the form of current page to page of another web application.

WebApp2::SecondForm.aspx.cs
if (Request.Form["TextBox1"]!=null)
TextBox1.Text = Request.Form["TextBox1"].ToString();

Please note that you also have to set EnableViewStateMac property as false in WebApp2::SecondForm.aspx page, else you will get error “The viewstate is invalid for this page and might be corrupted.”

WebApp2::SecondForm.aspx
<%@ Page language="c#" Codebehind="SecondForm.aspx.cs" AutoEventWireup="false" Inherits="WebApp2.WebForm1" EnableViewStateMac=false %>

When user click on the “buttonPassValue” button of WebApp1::FirstForm.aspx page, form is submitted to the page of another application WebApp2::SecondForm.aspx where the value of TextBox1 textbox is fetch from the Request object.

Major benefit of this method compare to passing value through query string is that your application will no long work under the constraint of maximum allowed characters in URL. With that pass value will not exposed to user in browser after navigating to next page. And hence you rarely have to bother about validating passed arguments in target application. However, I do recommend that you should validate sensitive data after pulling from Request object, as it’s possible to temper with form data before posting in previous application .