I tried creating helper class for WebMatrix on Visual Studio 2010.
1. Creating a new Class Library (.NET Framework 4)
2. Add System.Web.dll to reference.
3. Write some code you need, like the following
using System;
using System.Web;
public static class MyWebSite
{
private static string _URL = "http://www.bing.com/";
public static IHtmlString LinkToHome()
{
return LinkTo(_URL, "Home", "_self");
}
public static IHtmlString LinkTo(string url, string str, string target)
{
return new HtmlString("<a href=\"" + url + "\" target=\"" + target + "\">"
+ str + "</a>");
}
}
4. Copy the dll after compiled the above project to "bin" folder in your website on WebMatrix.
5. Call the Helper class under the rule of Razor syntax.
@MyWebSite.LinkToHome()
* The helper class also works fine for ASP.NET MVC 3 Web Application with Razor as view engine.
Plus, We can even create/use Custom CSHTML helper class directly by adding a C# class file or a CSHTML file in App_Code folder on WebMatrix.
Pattern 1: Using a C# file
1. creating Helper01.cs
using System; using System.Collections.Generic; using System.Web; /// <Summary> /// Summary description for Helper01 /// </Summary> public static class Helper01 { public static IHtmlString Message(string name) { return new HtmlString("Hello " + name); } }
2. usage:
@Helper01.Message("onamae")
Pattern 2: Using a CSHTML file
1. creating Helper02.cshtml
@helper Message(string name)
{
<text>Hello @name</text>
}
2. usage:
@Helper02.Message("seimei")
No comments:
Post a Comment