Building HtmlHelper Extension Methods for ASP.NET MVC

ASP.NET MVC is the latest buzz pattern in web development. ASP.NET MVC was recently released and I think it is a great way to develop web applications. If you are new to ASP.NET MVC, check ASP.NET MVC - Some Frequently Asked Questions and  Your First ASP.NET MVC Application. One of the benefits of MVC is you having total control over what and how your code is rendered in the browser. When you create the HTML for your page, a class that is very helpful is the HtmlHelper class. This class represents support for rendering HTML controls in a view. You can use the HtmlHelper class to render controls such as a TextBox, which renders as input type=”text”, to the browser like the following:
<%= Html.TextBox("Firstname") %>
In the first release of ASP.NET MVC, the HtmlHelper class does not cover a large area of HTML controls. This article will show you how to create your own extension method to complement the existing HtmlHelper class. Before going any further you’ll need to install ASP.NET MVC. You can download that here .
To begin with open Visual Studio 2008 and choose File > New > Project > ASP.NET MVC Web Application.
NewProject
 
By default Visual Studio creates several folders for you, namely Models, Views and Controllers. Start by creating a new folder in the root directory called Common. In that folder create a new class called Helpers and add the following code:
 
C#
 
public static class Helpers
{
       public static string Span(this HtmlHelper html, string text)
       {
            var builder = new TagBuilder("span");
            builder.GenerateId("firstName");
            builder.SetInnerText(text);
            return builder.ToString(TagRenderMode.Normal);
       }       
 }
 
VB.NET
 
Public Module Helpers
            <System.Runtime.CompilerServices.Extension> _
            Public Function Span(ByVal html As HtmlHelper, ByVal text As String) As String
                  Dim builder = New TagBuilder("span")
                  builder.GenerateId("firstName")
                  builder.SetInnerText(text)
                  Return builder.ToString(TagRenderMode.Normal)
            End Function
End Module
 
I’m using a feature in C# 3.0 called extension methods. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Using this new feature allows you to write custom methods that will appear in Visual Studio’s intellisense when working in the HTML designer. The ASP.NET MVC framework includes a useful utility class named the TagBuilder class that you can use when building HTML helpers. The TagBuilder class, as the name of the class suggests, enables you to easily build HTML tags. The next step is to make this helper class available to all the Views in the project. You could add this directive to each view:
 
<%@ Import Namespace="MvcApplication1.Common" %>
 
But that would mean you would need to add this directive to every view that needs to use this helper class. That isn’t acceptable because of maintenance. What happens if you want to move it to another namespace? Allot of find and replaces! The better option is to add the namespace to the web.config file so it’s available to every view. Add the following code to the pages/namespaces element:
 
<add namespace="MvcApplication1.Common"/>
 
This helper class can now be used through the entire project. To demonstrate how to use this helper class you need to open the About.aspx in the Home view and add the following code:
 
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>About</h2>
    <p>       
        <%= Html.Span("About page...") %>       
    </p>
</asp:Content>
 
In the code above there is the Span extension method created earlier. As you might expect it will render a <span> tag in the browser. Pretty neat huh?!
 

You can create helper methods that map to different HTML tags, so you can create them to display bold or even italic text. The choice is up to you. This is a nice feature of MVC that allows you to easily build custom HTML tags in your views. The entire source code of this article can be downloaded over here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET MVC 是一种基于模型-视图-控制器模式的 Web 应用程序框架,具有高性能和易于开发的优点。同时,它也提供了一种多语言的解决方案,可以实现应用程序的国际化和本地化。 下面我们来介绍一下 ASP.NET MVC 的多语言例子: 首先,我们需要在应用程序中准备多语言资源文件,这些资源文件包含了不同语言的文本信息和翻译。 接着,在 ASP.NET MVC 中,我们可以使用 Resx 功能来管理多语言资源。Resx 是一种基于 XML 的文件格式,它可以存储.NET 程序中使用的本地化数据。Resx 文件可以存储不同语言的字典信息。 例如,我们可以在应用程序的 Resx 文件中添加一个名称为“Hello”的键,然后为每个语言添加不同的本地化值。 之后,我们可以使用 ASP.NET MVC 视图中的内置功能将这些本地化资源引用到我们的应用程序中。在视图中使用 HTML.Helper 和@Html 中的 Resources 方法可以轻松地从资源文件中获取文本信息并显示到视图中,实现不同语言场景下的信息展示。 在控制器中,我们可以通过实例化不同的本地化资源管理器来获取特定语言下的资源文件,一般来说,我们可以根据不同语言的路径或者Cookie等方式来确定用户所选择的语言,然后动态加载相应的本地化资源文件。 这样,在实现了多语言资源和本地化功能后,用户可以根据自己的语言选择来体验应用程序,从而实现应用程序的国际化和本地化。同时,使用 ASP.NET MVC 的多语言功能也极大地提高了应用程序的性能,使得应用程序能够更加快速、高效地运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值