简体   繁体   English

将数据添加到用户会话时,Asp.NET返回ActionResult

[英]Asp.NET return ActionResult while adding data to user session

I have a method similar to this one: 我有一个与此类似的方法:

 public ActionResult Index() 
 {    
    Session["test"] = GetData();
    return View(); 
 }

Because my GetData() method takes too long, I was wondering if i can do something async in order to return the View while GetData() is running and adding the object to user session. 因为我的GetData()方法花费的时间太长,所以我想知道是否可以执行一些异步操作 ,以便在GetData()运行并将对象添加到用户会话时返回View I already thought at splitting this method into 2 methods the first to return 'View()' and the second one to be called with ajax and just for adding object to session, but i want to use this as a last resort. 我已经考虑过将此方法拆分为2个方法,第一个方法返回“ View()”,第二个方法用ajax调用,仅用于将对象添加到会话中,但是我想将此作为最后的手段。

Thanks in advance! 提前致谢!

Because my GetData() method takes too long, I was wondering if i can do something async in order to return the View while GetData() is running and adding the object to user session. 因为我的GetData()方法花费的时间太长,所以我想知道是否可以执行一些异步操作,以便在GetData()运行并将对象添加到用户会话时返回View。

You definitely do not want to run long running query on initial page load . 您绝对不希望在初始页面加载时运行长时间运行的查询。 User might think the network connection was lost, and keeps pressing F5 to refresh the page. 用户可能会认为网络连接已丢失,并一直按F5刷新页面。

Instead, you want two action methods - 相反,您需要两种操作方法-

  1. 1st Action Method returns regular view. 第一个操作方法返回常规视图。 Then at client-side, you make Ajax request to Action Method 2. 然后在客户端,您向操作方法2发出Ajax请求。

  2. 2nd Action Method returns JSON response 第二操作方法返回JSON响应

FYI: You want to make sure 2nd Action Method requires user login to access. 仅供参考: 您想确保第二种操作方法要求用户登录才能访问。 Otherwise, you might encounter DDoS attack. 否则,您可能会遇到DDoS攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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