简体   繁体   中英

ASP.NET MVC Call Extension Method in VB.NET Razor view

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM