windowsmodel-view-controllerclientwebget

Pass parameters to web page from Windows client


I have the following method in a controller in an MVC website:

[WebGet]
public void SaveInfo()
{
    string make = Request.QueryString["Make"];
    string model = Request.QueryString["Model"];

    // Do stuff....
}

It works fine when I type the URL in the address bar, but what I need to do is call this from a Windows client. How do I do this in C#?

Thanks!


Solution

  • Use the WebClient class to make an HTTP request from the client. You'll need to add a reference to System.Web if your client project doesn't already have one.

    using System.Net;
    using System.Web;
    
        static void SaveInfo(string make, string model)
        {
            using (WebClient webClient = new WebClient())
            {
                string response = webClient.DownloadString(
                    String.Format(
                        "http://yoursite/ControllerName/SaveInfo?make={0}&model={1}",
                        HttpUtility.UrlEncode(make),
                        HttpUtility.UrlEncode(model)
                    )
                );
            }
        }