gpt4 book ai didi

c# - 在 C# 中从控制台应用程序内部调用一个方法到我的 Web 应用程序

转载 作者:太空宇宙 更新时间:2023-11-03 23:04:34 24 4
gpt4 key购买 nike

我需要在 C# 中调用控制台到 Web 应用程序方法。

我的代码如下-

控制台应用程序代码:

public static class Program
{
public static void Main(String[] args)
{
Console.Write("Waiting for call getData method...");
Console.Read();
}
}

网络应用程序代码:

public class MyController : BaseController
{

[HttpGet]
public string getData(string data)
{
return data;
}
}

我只需要从控制台调用 getData(data) 方法。

请提出建议。

最佳答案

您可以使用 system.net.http 执行此操作。看看这篇 MSDN 文章。

Calling WebAPI from Console app

不要进入 RunAsync 和所有。只需使用 HTTPClient 调用 API。

这是完整的代码示例。.....................................

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace HttpClientSample
{
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}

class Program
{
static HttpClient client = new HttpClient();

static void ShowProduct(Product product)
{
Console.WriteLine($"Name: {product.Name}\tPrice: {product.Price}\tCategory: {product.Category}");
}

static async Task<Uri> CreateProductAsync(Product product)
{
HttpResponseMessage response = await client.PostAsJsonAsync("api/products", product);
response.EnsureSuccessStatusCode();

// return URI of the created resource.
return response.Headers.Location;
}

static async Task<Product> GetProductAsync(string path)
{
Product product = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<Product>();
}
return product;
}

static async Task<Product> UpdateProductAsync(Product product)
{
HttpResponseMessage response = await client.PutAsJsonAsync($"api/products/{product.Id}", product);
response.EnsureSuccessStatusCode();

// Deserialize the updated product from the response body.
product = await response.Content.ReadAsAsync<Product>();
return product;
}

static async Task<HttpStatusCode> DeleteProductAsync(string id)
{
HttpResponseMessage response = await client.DeleteAsync($"api/products/{id}");
return response.StatusCode;
}

static void Main()
{
RunAsync().Wait();
}

static async Task RunAsync()
{
client.BaseAddress = new Uri("http://localhost:55268/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

try
{
// Create a new product
Product product = new Product { Name = "Gizmo", Price = 100, Category = "Widgets" };

var url = await CreateProductAsync(product);
Console.WriteLine($"Created at {url}");

// Get the product
product = await GetProductAsync(url.PathAndQuery);
ShowProduct(product);

// Update the product
Console.WriteLine("Updating price...");
product.Price = 80;
await UpdateProductAsync(product);

// Get the updated product
product = await GetProductAsync(url.PathAndQuery);
ShowProduct(product);

// Delete the product
var statusCode = await DeleteProductAsync(product.Id);
Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})");

}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.ReadLine();
}

}
}

关于c# - 在 C# 中从控制台应用程序内部调用一个方法到我的 Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41775884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com