知乎周源微信_每周源代码30-具有XML文字的VB.NET作为ASP.NET MVC的视图引擎

本文讨论了VB9的XML字面量支持如何用于创建更友好的ASP.NET MVC视图。作者比较了传统的ASPX页面与使用VB9 XML字面量的方法,并展示了如何通过减法编码实现这一概念。此外,还提到了安德鲁·戴维的实现,他的代码利用了编译、强类型和IntelliSense特性。文章最后鼓励读者分享自己在视图和视图引擎方面创新或独特的方法。
摘要由CSDN通过智能技术生成
知乎周源微信

知乎周源微信

I was literally in the middle of writing the post when I saw a message from Andrew Davey about how he had implemented the same idea! Of course, his is way better, so I got to code via subtraction. That means subtracting out the crap I had written in a few minutes and dig into his code.

当我看到安德鲁·戴维( Andrew Davey)关于他如何实现相同想法的消息时,我确实在写这篇文章。 当然,他的方法更好,所以我必须通过减法进行编码。 这意味着要减去我几分钟内写的废话,然后深入研究他的代码。

There are no unique ideas, right? ;) Either way, it's fun when the same idea is being thought about simultaneously.

没有独特的主意吧? ;)无论哪种方式,同时考虑同一想法都会很有趣。

Here's the general concept. A few weeks back I was talking with Avner Aharoni, a Language Program Manager, and he had been kicking around the idea of VB9's XML Literals making friendlier Views within ASP.NET MVC.

这是一般概念。 几周前,我正在与语言程序经理Avner Aharoni进行交谈,他一直在探讨VB9的XML Literal在ASP.NET MVC中创建更友好的视图的想法。

I've blogged about VB9's rocking sweet XML support before. It lets you create XML like this. Note the lack of strings...the XML is there in the language and the compiler and intellisense are all aware of it.

之前,我已经写过关于VB9出色的XML支持的博客。 它使您可以像这样创建XML。 注意缺少字符串...语言中存在XML,编译器和intellisense都知道它。

 Dim books = <bookstore xmlns="http://examples.books.com">
<book publicationdate=<%= publicationdate %> ISBN=<%= isbn %>>
<title>ASP.NET Book</title>
<price><%= price %></price>
<author>
<first-name><%= a.FirstName %></first-name>
<last-name><%= a.LastName %></last-name>
</author>
</book>
</bookstore>

ASP.NET MVC中的视图 (Views in ASP.NET MVC)

Starting with the Northwind ASP.NET MVC Sample Code for Preview 3 that Phil updated, let's look at the List View:

Phil更新的预览版3Northwind ASP.NET MVC示例代码开始,让我们看一下列表视图:

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<h2><%=ViewData.CategoryName %></h2>
<ul>
<% foreach (Product product in ViewData.Model.Products.Model) { %>
<li id="prod<%= product.ProductID %>">
<%= product.ProductName %>
<span class="editlink"">
(<%= Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })%>)
</span>
</li>
<% } %>
</ul>
<%= Html.ActionLink("Add New Product", new { Action="New" }) %>
</asp:Content>

This is straight from the ASPX page with inline C#.

这直接来自带有内联C#的ASPX页面。

使用VB9的XML文字支持的ASP.NET MVC视图 (ASP.NET MVC Views using VB9's XML Literal Support)

image

Now, we thought it'd be cool/interesting/potentially-something if we could use the XML Literal support to get, as Andrew puts it "compiled, strongly typed, intellisense friendly views." Sure, we mostly get that with the ASPX pages, but perhaps this would be better/easier/something? Keep in mind here that we're playing.

现在,我们认为如果可以使用XML Literal支持来获取(如安德鲁所说)“很酷,很有趣/很有可能”,就像安德鲁所说的那样:“经过编译,强类型,具有智能感知的视图”。 当然,我们大多数都是通过ASPX页面得到的,但这也许会更好/更容易/更合适? 请记住,我们正在玩。

Your opinions on if this is a good idea or something to move forward would be useful. Leave comments and I'll compile and give them directly on to the VB and MVC team(s)! Remember, it can look like however you think it should, so don't be constrained by my spike or Andrew's.

您对于这是一个好主意还是要前进的观点将很有用。 留下评论,我将进行编译并将其直接交给VB和MVC团队! 请记住,它看起来像您认为应该的那样,所以不要被我的尖峰或安德鲁的约束。

Here's why Andrew thinks it's cool, quoted from a post on the ALT.NET mailing list:

从ALT.NET邮件列表中的帖子中引用安德鲁认为这很酷的原因:

Some key features I've used.
- <%= From ... Select ... %> to create repeated elements
- VB's ternary "If" operator for conditional output
- X-linq to post process the HTML before sending it
- choose between indented and compressed XML output
- modules of functions as "controls" - it's so simple :)

我使用过的一些关键功能。 -<%= From ...选择...%>以创建重复的元素-VB的三元“ If”运算符,用于条件输出-X-linq在发送HTML之前对其进行后期处理-在缩进和压缩的XML输出之间选择-作为“控件”的功能模块-非常简单:)

Here's what my solution looks like in Visual Studio. See how the ListVB.aspx has no "+" sign. There's no code-behind .cs file there even though this is a C# project. The meat of the View is in another assembly (although you could conceivably do something freaky and get VB and C# to live in the same assembly (be sure to read the comments)).

这是我的解决方案在Visual Studio中的外观。 查看ListVB.aspx如何没有“ +”号。 即使这是C#项目,也没有在.cs文件后面的代码。 View的内容在另一个程序集中(尽管可以想象,您可以做一些怪异的事情,并使VB和C#驻留在同一程序集中(请务必阅读注释))。

Actually, the ListVB.aspx file is VB, not C# and refers not to to a code-behind, but another class in another DLL, specifically the VBViews assembly.

实际上,ListVB.aspx文件是VB,而不是C#,并且不是引用到后台代码,而是引用另一个DLL中的另一个类,尤其是VBViews程序集。

<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" 
AutoEventWireup="true" Inherits="VBViews.ListVB" Title="Products" %>
<%@ Import Namespace="NorthwindModel" %>
<%@ Import Namespace="System.Collections.Generic" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2><%=ViewData.Model.CategoryName%></h2>
<% = GetList() %>
</asp:Content>

Here's the Visual Basic code in the other assembly.

这是另一个程序集中的Visual Basic代码。

Imports System.Xml.Linq
Imports NorthwindDemo.Models

Partial Public Class ListVB
Inherits System.Web.Mvc.ViewPage

Function GetList() As XElement
Dim c As Category = CType(ViewData.Model, Category)
Return <ul><%= From product In c.Products _
Select _
<li id=<%= "prod" & product.ProductID %>>
<span class="editlink">
<a href=<%= "/Products/Edit/" & product.ProductID %>>
<%= product.ProductName %>
</a>
</span>
</li> %>
</ul>
End Function
End Class

This won't really be clear without some syntax highlighting to make the point, so here it is again, but this time as a screenshot. See now the VB code, XML and <% %> blocks are all together in same line? VB is just generating XElement's which in turn will turn into a string when "rendered" by the ASPX page.

如果没有突出显示语法,这实际上并不清楚,所以再次出现,但这一次是屏幕截图。 现在看到VB代码,XML和<%%>块都在同一行中吗? VB只是生成XElement,当由ASPX页面“呈现”时,XElement又将变成字符串。

Andrew将VB9 XML文字列为ASP.NET MVC视图的第二名 (Andrew's Take #2 on the VB9 XML Literals as ASP.NET MVC Views)

Andrew Davey's NRest project is more than just VB9 Views. It's a REST web framework for ASP.NET using the Nemerle programming language (using the May CTP). You can browser or GET his code with SVN here: http://svn.assembla.com/svn/nrest/trunk/. It's also a nicely laid out solution that uses the Ninject IOC but I'll cover that later. Do check out Andrew's screencast about his NRest project.

Andrew Davey的NRest项目不仅仅是VB9视图。 它是使用Nemerle编程语言(使用May CTP)的ASP.NET REST Web框架。 您可以在以下位置使用SVN浏览器或获取他的代码: http : //svn.assembla.com/svn/nrest/trunk/ 。 这也是一个使用Ninject IOC的布局很好的解决方案,但稍后会介绍。 请查看安德鲁关于NRest项目的截屏视频

His code is a mix of C#, Nemerle and VB. The Website, Tests and Services are in C# and the Ninject modules are in Nemerle, along with the meat of the main NRest project. I think he could have used more of System.MVC, specifically the View Engines, that he did, but I'm still grokking his intent.

他的代码混合了C#,Nemerle和VB。 网站,测试和服务均位于C#中,Ninject模块位于Nemerle中,以及主要NRest项目的内容。 我认为他本可以使用更多的System.MVC,尤其是View Engines,但我仍然对他的意图不满意。

He's got a hierarchy in VB with a MainPageBase, Page, in order to achieve a kind of Master Pages:

为了获得一种母版页,他在VB中拥有一个MainPageBase的层次结构,即页面:

Public Class MainPageBase(Of TChrome As MainChrome, TContent)
Inherits Page(Of TChrome, TContent)

Public Overrides Function GetHtml() As XElement
Return _
<html>
<head>
<title><%= Chrome.Title %></title>
<link href="/styles/demo.css" type="text/css" rel="Stylesheet"/>
<%= GetHeadContents().Elements() %>
</head>
<body>
<h1><%= Chrome.Title %></h1>
<%= GetBodyContents().Elements() %>
</body>
</html>
End Function

Public Overridable Function GetHeadContents() As XElement
Return <_></_>
End Function

Public Overridable Function GetBodyContents() As XElement
Return <_></_>
End Function

End Class

So a Hello World page in VB would be very simple, just this:

因此,VB中的Hello World页面将非常简单,仅此:

Public Class CustomerPage
Inherits MainPageBase(Of CustomerPageData)

Public Overrides Function GetBodyContents() As XElement
Return _
<_>
<p>Hello <%= Content.FirstName & " " & Content.LastName %></p>
</_>
End Function
End Class

All of this is a work in progress, but it's really cool that we're all trying to push the envelope and not afraid to try crazy stuff in order to make things better. It'll be cool for me to read this post in a year and either say "ew" or "cool!" depending on what direction we all went.

所有这一切仍在进行中,但是我们都在努力突破极限,而不是为了提高条件而疯狂尝试疯狂的东西,这真是太酷了。 对我来说,一年内读这篇文章并说“ ew”或“ cool!”真是太酷了! 取决于我们所有人走的方向。

Have you done anything cool or crazy with Views and ViewEngines, Dear Reader?

亲爱的读者,您是否对Views和ViewEngines做过酷或疯狂的事情?

Related Posts

相关文章

翻译自: https://www.hanselman.com/blog/the-weekly-source-code-30-vbnet-with-xml-literals-as-a-view-engine-for-aspnet-mvc

知乎周源微信

ASP.NET MVC 5 框架揭秘》以一个模拟ASP.NET MVC内部运行机制的“迷你版MVC框架”作为开篇,其目的在于将ASP.NET MVC真实架构的“全景”勾勒出来。接下来本书以请求消息在ASP.NET MVC框架内部的流向为主线将相关的知识点串连起来,力求将”黑盒式”的消息处理管道清晰透明地展示在读者面前。相信精读本书的读者一定能够将ASP.NET MVC从接收请求到响应回复的整个流程了然于胸,对包括路由、Controller的激活、Model元数据的解析、Action方法的选择与执行、参数的绑定与验证、过滤器的执行以及View的呈现等相关的机制具有深刻的理解。 本书以实例演示的方式介绍了很多与ASP.NET MVC相关的很好实践,同时还提供了一系列实用性的扩展,相信它们一定能够解决你在真实开发过程中遇到的很多问题。本书末章提供的案例不仅仅用于演示实践中的ASP.NET MVC,很多的架构设计方面的东西也包含其中。除此之外,本书在很多章节还从设计的角度对ASP.NET MVC的架构进行了深入分析,所以从某种意义上讲本书可以当成一本架构设计的书来读。 ASP.NET MVC 5 框架揭秘 目录 第1章 ASP.NET + MVC 第2章 路由 第3章 Controller的激活 第4章 Model元数据的解析 第5章 3个描述对象 第6章 Model的绑定(上篇) 第7章 Model的绑定(下篇) 第8章 Model的验证(上篇) 第9章 Model的验证(下篇) 第10章 Action方法的执行 第11章 View的呈现 第12章 过滤器 第13章 特性路由 第14章 案例实践
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值