Reputation: 1631
How to call Extension method in VB.NET Razor view. It is working in c# but not able to run it in VB.NET.
Note: Target Framework in .NET 4.0
Here is the Code:
@Imports ApplicationSupport.Models
@Html.RenderXml("XML String here", Server.MapPath("~/XSLT/Contents.xslt"))
And then Extension Method as below:
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Runtime.CompilerServices
Namespace ApplicationSupport.Models
Public Module HtmlHelperExtensions
<Extension()> _
Public Function RenderXml(helper As HtmlHelper, xml As String, xsltPath As String) As HtmlString
Dim args As New XsltArgumentList()
Dim t As New XslCompiledTransform()
t.Load(xsltPath)
Dim settings As New XmlReaderSettings()
settings.DtdProcessing = DtdProcessing.Parse
settings.ValidationType = ValidationType.DTD
Using reader As XmlReader = XmlReader.Create(New StringReader(xml), settings)
Dim writer As New StringWriter()
t.Transform(reader, args, writer)
Dim htmlString As New HtmlString(writer.ToString())
Return htmlString
End Using
End Function
End Module
End Namespace
And then getting error as below:
I was not able to find any useful help on it. I hope someone can point me in right direction.
I am c# programmer but this piece of code needed in VB.NET.
Upvotes: 1
Views: 562
Reputation: 1332
I think your import in the view needs to be
@Imports ApplicationSupport.Models.HtmlHelperExtensions
so it will import the module your extension method resides in.
Upvotes: 2