Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NETMVC:使用LINQtoXML呈现(X)HTML_Asp.net_Asp.net Mvc_Linq To Xml_Html Helper - Fatal编程技术网

ASP.NETMVC:使用LINQtoXML呈现(X)HTML

ASP.NETMVC:使用LINQtoXML呈现(X)HTML,asp.net,asp.net-mvc,linq-to-xml,html-helper,Asp.net,Asp.net Mvc,Linq To Xml,Html Helper,关于ASP.NET MVC的视图引擎,已经有很多讨论,对于带有for循环之类的内联“标记汤”也有一些批评 替代方法或补充方法是使用HTML帮助程序,它们只是内联方法调用 今天,当我查看ASP.NET MVC的HTML助手时,他们使用的是一个名为TagBuilder的类 我的建议是使用LINQ to XML来获得强类型和正确格式的(X)HTML: XDocument output = new XDocument(); XElement root = new XElement("div",

关于ASP.NET MVC的视图引擎,已经有很多讨论,对于带有for循环之类的内联“标记汤”也有一些批评

替代方法或补充方法是使用HTML帮助程序,它们只是内联方法调用

今天,当我查看ASP.NET MVC的HTML助手时,他们使用的是一个名为TagBuilder的类

我的建议是使用LINQ to XML来获得强类型和正确格式的(X)HTML

XDocument output = new XDocument();
XElement root = new XElement("div",
    new XAttribute("class", "root_item"));

XElement iconImage = new XElement("img",
    new XAttribute("src", ResolveUrl("~/image.gif")),
    new XAttribute("alt", "This is an image"));

XElement link = new XElement("a",
    new XAttribute("class", "link"),
    new XAttribute("href", "http://google.com"),
    new XText("Link to Google"));


root.Add(link);
root.Add(iconImage);
output.Add(root);
我喜欢它,因为它就像WebForms中的强类型控件,您可以在其中新建一个按钮并将其添加到另一个控件的控件集合中


这种方法有任何明显的问题或局限性吗?

对于上述方法,我可以想到两个问题。 首先,

根据您在上面写的内容,我们可以得到如下内容:

<img src=<%=whatever%> alt=<%=whatever%> />
alt=/>
这可能是个人的判断或什么,但我肯定投票后一个更“人性化”的可读性。没错,使用LINQ 2 XML可能会消除在我的aspx页面中徘徊的奇怪现象,但同时,你会让那些“好男孩”看起来很笨拙

其次可能是性能问题。我认为解析和执行LINQ2XML可能会非常慢,尽管我没有关于这方面的任何数据


就我个人而言,我仍在试验MVC框架,这感觉就像回到了ASP或PHP3.X那样的旧时代,因为几乎所有的交互部分都是显式处理的,而不是面向Windows/GUI-OOP的ASP.Net框架。我认为我使用MVC的主要原因是它可以保证最好质量的客户端HTML代码

这是个好主意!我看到的唯一问题是C的使用。)VB.NET通过其XML文本特性更好地支持生成XML

您在问题中列出的代码可以在VB.NET中这样编写。(添加文本“This is a link”,因为您的示例在
a
元素中不包含任何文本。)

Dim root=
alt=“这是一幅图像”/>
仍然有
标记,但会在编译时检查它们的有效性。如果此代码是返回类型XElement的函数的返回值,那么该Xhtml片段可以在站点的其他地方重用


我在CodePlex上有一个项目,它使用VB.NET XML文本作为自定义ASP.NET MVC视图引擎。它基于Microsoft的ASP.NET产品单元经理编写的代码。视图是VB.NET类,母版页是基类。您可以新建部分视图类,而不是通过名称字符串引用它们,因此这也是一个额外的编译时检查。与返回字符串的HtmlHelper类不同,有一个XhtmlHelper类返回XElement,其工作原理与您所建议的类似。

我个人不喜欢TagBuilder,在大多数情况下,我都求助于构建自己的类,实际上,它只不过是一个string.Format()。我喜欢你的方法,但你为什么不想使用TagBuilder呢?根据我的经验,Linq到XML的构建速度非常快——它永远不会成为您的瓶颈。如果与性能相关(假设),一般来说,字符串操作相当昂贵-直接使用对象构建xml树通常比在序列化树上执行字符串操作便宜(请注意,具有相同名称的元素可以共享对该名称的相同字符串引用)当然,ASP.NET会编译视图,因此它也避免了运行时解析+字符串格式化。。。
<img src=<%=whatever%> alt=<%=whatever%> />
Dim root = <div class="root_item">
               <img src=<%= ResolveUrl("~/image.gif") %> alt="This is an image"/>
               <a class="link" href="http://google.com">This is a link</a>
           </div>