Search code examples
c#windowsweb-applicationshttpclientblazor-webassembly

how to call a function in web page from windows application


I have an windows application and also web page application in two different solution and I want When any changes happen in windows application could automatically call "LoadData" method on web page so update page data. how should I do ? web application is ".net core blazor" window application is ".net framework c#" best regards.


Solution

  • Your windows application has to make an http call to your web application using the HttpClient class. Check this https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-6.0

    Here is the code from the link, I adjusted the URL to the web application to point to your page

    static readonly HttpClient client = new HttpClient();
    
    static async Task Main()
    {
        // Call asynchronous network methods in a try/catch block to handle exceptions.
        try 
        {
            HttpResponseMessage response = await client.GetAsync("http://you-site/your-page");
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            // Above three lines can be replaced with new helper method below
            // string responseBody = await client.GetStringAsync(uri);
    
            Console.WriteLine(responseBody);
        }
        catch(HttpRequestException e)
        {
            Console.WriteLine("\nException Caught!");   
            Console.WriteLine("Message :{0} ",e.Message);
        }
    }
    

    The line client.GetAsync("http://you-site/your-page"); will cause your-page to get loaded which will call the page_load function