使用JQuery或其他类似框架从自定义url /Web服务加载HTML内容非常容易。到目前为止,我已经多次使用这种方法,并发现性能令人满意。
但是所有的书,所有的专家都试图让我使用JSON而不是生成的HTML。它为什么比HTML更优越?
它会快很多吗?
它在服务器上的负载是否非常小?
另一方面,我有一些使用生成HTML的理由。
它是简单的标记,通常和JSON一样紧凑,甚至更紧凑。
它更不容易出错,因为你得到的都是标记,而不是代码。
在大多数情况下,编程会更快,因为你不必为客户端单独编写代码。
你站在哪一边,为什么?
I have something interesting I thought I might add. I developed an application that only ever loaded a full view one time. From that point forward it communicated back to the server with ajax only. It only ever needed to load one page (my reason for this is unimportant here). The interesting part comes in that I had a special need to return some data to be operated on in the javascript AND a partial view to be displayed. I could have split this up into two calls to two separate action methods but I decided to go with something a little more fun.
看看吧:
public JsonResult MyJsonObject(string someData)
{
return Json(new { SomeData = someData, PartialView = RenderPartialViewToString("JsonPartialView", null) }, JsonRequestBehavior.AllowGet);
}
你可能会问RenderPartialViewToString()是什么?就是这个小块的酷:
protected string RenderPartialViewToString(string viewName, object model)
{
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
我还没有做任何性能测试,所以我不确定它是否招致任何更多或更少的开销调用一个动作方法为JsonResult和一个为ParticalViewResult,但我仍然认为这是相当酷的。它只是将一个局部视图序列化为一个字符串,并将它与Json作为参数之一一起发送。然后,我使用JQuery来获取该参数,并将其打到适当的DOM节点:)
告诉我你对我的混血儿有什么看法!
JSON is very versatil and lightweight format. I have discovered its beauty when I have started to use it as a client side template parser data. Let me explain, while before I was using smarty and views on server side (generating high server load), now I use some custom jquery functions and all the data is rendered on client side, using clients browser as template parser. it saves server resourses and on another hand browsers improve their JS engines every day. So the speed of client parsing is not an important issue right now, even more, JSON objects are ususally very small so they don't consume a lot of client side resourses. I prefer to have a slow website for some users with slow browser rather than slow site for everyone because of very loaded server.
另一方面,从服务器发送纯数据,你可以从表示中抽象它,所以如果明天你想改变它或将你的数据集成到另一个服务中,你可以更容易地做到这一点。
这只是我的个人意见。
I have something interesting I thought I might add. I developed an application that only ever loaded a full view one time. From that point forward it communicated back to the server with ajax only. It only ever needed to load one page (my reason for this is unimportant here). The interesting part comes in that I had a special need to return some data to be operated on in the javascript AND a partial view to be displayed. I could have split this up into two calls to two separate action methods but I decided to go with something a little more fun.
看看吧:
public JsonResult MyJsonObject(string someData)
{
return Json(new { SomeData = someData, PartialView = RenderPartialViewToString("JsonPartialView", null) }, JsonRequestBehavior.AllowGet);
}
你可能会问RenderPartialViewToString()是什么?就是这个小块的酷:
protected string RenderPartialViewToString(string viewName, object model)
{
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
我还没有做任何性能测试,所以我不确定它是否招致任何更多或更少的开销调用一个动作方法为JsonResult和一个为ParticalViewResult,但我仍然认为这是相当酷的。它只是将一个局部视图序列化为一个字符串,并将它与Json作为参数之一一起发送。然后,我使用JQuery来获取该参数,并将其打到适当的DOM节点:)
告诉我你对我的混血儿有什么看法!