.net mvc设计ajax调用(在哪里放置ajax方法)
你好 从 ASP.NET 迁移到 MVC ASP.NET 时,MVC 看起来对 AJAX 更加友好。 但我仍然遇到设计问题, 有人知道微软在调用AJAX方法时的设计意图吗? 默认情况下,我需要将此方法放在哪里,在单独的控制器中,同一个控制器中? 有相关的官方信息吗?
谢谢
Hi
When migrating from ASP.NET to MVC ASP.NET it looks like the MVC is more AJAX friendly.
but still I run into design issue,
Does someone knows Microsoft intention about the design when calling AJAX methods?
Where do I need to put this methods by default, in a separate controller, the same controller?
Is there any kind of official info about it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为没有任何官方最佳实践。就我个人而言,我喜欢在组织控制器和操作时遵循 RESTful 约定,无论这些操作如何使用(无论是否使用 AJAX)。
I don't think that there is any official best practices. Personally I like to follow RESTful conventions when organizing cotrollers and actions no matter how those actions are consumed (AJAX or not).
您可能想尝试查看此处的一些 asp.net 示例。这会给你一些想法。 :)
You might wanna try on having a review on some asp.net samples here. This will give you some ideas. :)
我建议在选择方法放置时考虑 SessionState。
例如,如果我在常规操作中使用会话状态,而不是在 ajax 操作中使用会话状态(这对我来说很有意义),我会将 ajax 操作移动到单独的控制器中,并且我希望 ajax 方法异步执行。然后我将这些 ajax 方法放在单独的控制器中,并将控制器标记为 [SessionState(SessionStateBehavior.Disabled)] (或 ReadOnly)。我发现这在性能方面有很大的进步。
请注意,当您使用 TempData、ViewData 或 ViewBag 变量时,您就使用了 Session。
SessionState 的解释如下:
ASP.NET MVC 和 Ajax,并发请求?
I would suggest taking SessionState into consideration when making the choice of method placement.
For instance, I would move ajax actions into a separate controller if I am using session state in regular actions, but not in ajax actions (which makes sense for me) and I would like the ajax methods to execute asynchronously. Then I put those ajax methods in a separate controller and mark the controller as [SessionState(SessionStateBehavior.Disabled)] (or ReadOnly). I have found this to be a great improvement in terms of performance.
Note, that you use Session when you use TempData, ViewData or ViewBag variables.
SessionState is explained here:
ASP.NET MVC and Ajax, concurrent requests?