简体   繁体   中英

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.

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/do.net/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM