Javascript 如何从web服务获取到ASP.NET MVC视图的通知

Javascript 如何从web服务获取到ASP.NET MVC视图的通知,javascript,c#,asp.net-mvc,web-services,signalr,Javascript,C#,Asp.net Mvc,Web Services,Signalr,任务: 向数据库添加一些数据-大约5分钟 向客户端发送通知“数据已添加到数据库” 过程数据-约15分钟 向客户端发送“数据已处理”通知 代码: ASMX网络服务 [SoapDocumentMethod(OneWay = true)] [WebMethod] public void AddAndProcess(DataSet _DataToProcess) { //inserts data to DB SendNotification("Data added to

任务:

  • 向数据库添加一些数据-大约5分钟
  • 向客户端发送通知“数据已添加到数据库”
  • 过程数据-约15分钟
  • 向客户端发送“数据已处理”通知
代码: ASMX网络服务

[SoapDocumentMethod(OneWay = true)]
[WebMethod]
public void AddAndProcess(DataSet _DataToProcess)
{
    //inserts data to DB

    SendNotification("Data added to database");

    ProcessData(_DataToProcess);
}

[SoapDocumentMethod(OneWay = true)]
[WebMethod]
public void ProcessData(DataSet _DataToProcess)
{
    //Process data

    SendNotification("The data is processed");
}

public void SendNotification(string NotificationMessage)
{
    //do something to send a notification to client
}
ASP.NET MVC视图

@using (Html.BeginForm("AddAndProcess", "DataProcessor", FormMethod.Post, new {@class = "form-horizontal", role = "form", enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()

<h1>Upload data file</h1>

    <div class="form-group">
        <div class="col-md-10">
            @Html.Label("Select data file", new { @class = "col-md-4 control-label" })
            @Html.TextBox("file", null, new { type = "file" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @Html.TextBox("Submit", "Process", new { type = "submit" })
        </div>
    </div>
    }
说明:

我有一个ASP.NET MVC视图,需要在该视图上显示函数执行状态通知,如上所示

为了节省用户的时间,web服务被标记为。在这种情况下,我无法返回NotificationMessage字符串并在视图中显示

问题:


如何将通知从ASMX web服务发送到ASP.NET MVC视图?

为什么使用ASMX?为什么不通过MVC或Web API公开和访问端点?如果您试图在ASP.NET中将通知从服务器推送到客户端,这可能是您最好的选择。我在评论中链接到了SignalR。那里有很多文档。我认为这已经足够了,你可以尝试一下,然后再回来,如果你在实现的某个特定部分陷入困境。是的,当然@mason!这将是一门新的学问。由于这对我来说是新的,我实际上无法为这个问题制定解决方案。做完作业后我会回来的。ASMX无法向客户端发送数据。客户端可以从ASMX检索数据。因此,如果您死气沉沉地使用ASMX,那么最终会让客户端不断轮询ASMX,说“嘿,给我发送所有通知”。99%的时候,ASMX会说“对不起,我没有给你任何东西”。而signar,服务器可以轻拍客户端的肩膀说“嘿,这里有一些通知。”更不用说闲聊了,代码也更接近于消息应该如何从服务器流向客户端的图表。Web API在ASP.NET 5中。WCF不是。因此,你可以看到哪一个似乎得到更多的关注。但这也取决于你需要它做什么。WebAPI可以通过HTTP实现JSON和XML,但WCF在格式和传输方面要灵活得多。如果您不需要所有这些功能,我会选择WebAPI。
public class DataProcessor : Controller
{
    public ActionResult AddAndProcess()
    {
        //Call data processor web services to 
        //1. Add some data to database - approx 5 minutes
        //2. Send a notification to client "Data added to database"
        //3. Process data - approx 15 minutes
        //4. Send a notification to client "The data is processed"
        return View();
    }
}