如何从外部应用程序(不使用 SignalR)启动 SignalR hub
How to fire off SignalR hub from external application (that doesn't use SignalR)
我有一个 Asp.Net MVC 4 应用程序(应用程序 A),它将使用 SignalR 为用户提供实时更新。由于我们正在使用较旧的 .net 框架版本,我正在使用 SignalR 版本 1.1.4。
有一个外部应用程序(应用程序B),当提交订单时,我想通知应用程序A发送订单通知。
我最初的想法是创建一个 ASP.NET Web 服务来托管 SignalR,但是由于应用程序 B 不会使用 SignalR,我认为只要调用应用程序 A 的控制器传递必要的数据就可以了需要什么。
那么,从应用程序 B,我将如何调用应用程序 A 的控制器来传递必要的数据? Ajax 可以用来调用外部应用程序吗?如果是这样,控制器方法会是什么样子?
这些都是具有 windows 身份验证的 Intranet 应用程序。
I figure that just making a call to the controller of application A
passing necessary data will work for what is needed
您可以在应用程序 B 中使用 HttpClient 来调用应用程序 A 中的控制器操作。
创建订单并将订单发送到另一个 MVC 应用程序控制器的示例(未测试)。
private HttpClient client = new HttpClient();
public HomeController()
{
client.BaseAddress = new Uri("http://localhost:49277");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
[HttpPost]
public ActionResult Insert(Order order)
{
HttpResponseMessage response = client.PostAsJsonAsync<Order>("/Order/Post" + $"/{order.OrderID}", order).Result;
return RedirectToAction("Index");
}
编辑
可以直接使用-UseDefaultCrendentials or pass the credentials。
using (var handler = new HttpClientHandler {UseDefaultCredentials = true})
{
using (var client = new HttpClient(handler))
{
...
}
}
或
var credentials = new NetworkCredential(userName, password);
using (var handler = new HttpClientHandler {Credentials = credentials })
{
using (var client = new HttpClient(handler))
{
...
}
}
我有一个 Asp.Net MVC 4 应用程序(应用程序 A),它将使用 SignalR 为用户提供实时更新。由于我们正在使用较旧的 .net 框架版本,我正在使用 SignalR 版本 1.1.4。
有一个外部应用程序(应用程序B),当提交订单时,我想通知应用程序A发送订单通知。
我最初的想法是创建一个 ASP.NET Web 服务来托管 SignalR,但是由于应用程序 B 不会使用 SignalR,我认为只要调用应用程序 A 的控制器传递必要的数据就可以了需要什么。
那么,从应用程序 B,我将如何调用应用程序 A 的控制器来传递必要的数据? Ajax 可以用来调用外部应用程序吗?如果是这样,控制器方法会是什么样子?
这些都是具有 windows 身份验证的 Intranet 应用程序。
I figure that just making a call to the controller of application A passing necessary data will work for what is needed
您可以在应用程序 B 中使用 HttpClient 来调用应用程序 A 中的控制器操作。
创建订单并将订单发送到另一个 MVC 应用程序控制器的示例(未测试)。
private HttpClient client = new HttpClient();
public HomeController()
{
client.BaseAddress = new Uri("http://localhost:49277");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
[HttpPost]
public ActionResult Insert(Order order)
{
HttpResponseMessage response = client.PostAsJsonAsync<Order>("/Order/Post" + $"/{order.OrderID}", order).Result;
return RedirectToAction("Index");
}
编辑
可以直接使用-UseDefaultCrendentials or pass the credentials。
using (var handler = new HttpClientHandler {UseDefaultCredentials = true})
{
using (var client = new HttpClient(handler))
{
...
}
}
或
var credentials = new NetworkCredential(userName, password);
using (var handler = new HttpClientHandler {Credentials = credentials })
{
using (var client = new HttpClient(handler))
{
...
}
}