0

我正在使用 MVC3 和 MEF 创建一个 Web 应用程序。我正在尝试使用 IDNumber 将我的 HostModel 导出到插件,然后让插件重定向到使用该 IDNumber 的链接。我的模型没有正确导出,然后我的视图也没有从插件控制器读取模型(我通过创建一个测试模型对象并将其传递给视图来测试它)。我很确定我与我在插件中导出到视图的方式有些混淆。我使用的是 .aspx 文件而不是 .vbhtml,因为 VS2010 没有给我选项。当我尝试将 .vbhtml 文件拖到项目中时,它无法正常工作。

这是我的主机控制器:

<Export(GetType(HostModel))>
<PartCreationPolicy(CreationPolicy.NonShared)>
Public Class HostController
    Inherits System.Web.Mvc.Controller


    Private m_objHost As HostModel

    Private m_IDNUmber As String
    Property IDNumber() As String
        Get
            Return m_IDNUmber
        End Get
        Set(value As String)
            m_IDNUmber = value
        End Set
    End Property


    Function Index() As ActionResult
        ViewData("Message") = "Welcome to ASP.NET MVC!"
        If m_objHost Is Nothing Then
            m_objHost = New HostModel
        End If
        Return View(m_objHost)
    End Function

    '<HttpPost()>
    Function ChangeCUNumber(model As HostModel, strIDNumber As String) As ActionResult
        ' m_IDNUmber = strIDNumber
        model.IDNumber = strIDNumber
        Return View("Index", model)
    End Function

    <HttpPost()>
    Function GoToMini(model As HostModel) As ActionResult
        m_CUNUmber = model.CUNumber
        Dim hostContollerObj As New HomeController
        hostContollerObj.CUNumber = model.IDNumber

        m_objHost = model
        Return Redirect("http://localhost:3727/miniView")
    End Function
End Class

这是我的主机模型:

Public Class HostModel
Implements IHost

Private Shared m_instance As HostModel
Private m_IDNumber As String

Public Sub New()
End Sub



Shared ReadOnly Property instance() As HostModel
    Get
        If m_instance Is Nothing Then
            m_instance = New HostModel
        End If
        Return m_instance
    End Get
End Property


Public Property IDNumber As String Implements CUCMCV_Interfaces.IHost.IDNumber
    Get
        Return m_IDNumber
    End Get
    Set(value As String)
        m_IDNumber = value
    End Set
End Property

这是我的插件控制器:

<Export(GetType(IPlugin))> _
<ExportMetadata("PluginName", "miniView")> _
<PartCreationPolicy(CreationPolicy.NonShared)> _
Public Class miniViewController
    Inherits System.Web.Mvc.Controller
    Implements IPlugin


    <Import(GetType(HostModel))>
    Private m_objHost As HostModel


    Public Function Index() As ActionResult
        Dim renderedView As ViewResult = View("~/Plugin/miniView.dll/miniView.Index.aspx", m_objHost)

        Return renderedView
    End Function

这是我的插件视图(Index.aspx)

Public Class Index
    Inherits Mvc.ViewPage




    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        Dim p As ImportData = New ImportData()
        Dim strMINILink As String = "http://inside/mini?ContractNumber="
        Dim strCUNumber As String = p.instance.IDNumber
        Dim strMINIURL As String = strMINILink & strIDNumber
        Response.Redirect(strMINIURL)

    End Sub

End Class

Public Class ImportData

    <Import(GetType(HostModel))>
    Property instance As HostModel

    Public Sub New()
        Dim catalog As AggregateCatalog = New AggregateCatalog()

        catalog.Catalogs.Add(New DirectoryCatalog("C:\Documents and Settings\gbv0860\My Documents\cucmConsolidatedView\CUCMCV\cucmConsolidatedView\cucmConsolidatedView\bin"))
        Dim _container As CompositionContainer = New CompositionContainer(catalog)

        Try
            _container.ComposeParts(Me)

        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try

    End Sub
End Class

任何帮助将非常感激!如果您需要我澄清任何事情,请告诉我!谢谢!

4

1 回答 1

1

您已经<Export(GetType(HostModel))>上了HostController实际上不是HostModel. 这不是一个有效的出口。

ASP.NET MVC 将按控制器的确切类型请求控制器,因此您应该HostController只使用<Export>.

<Export(GetType(HostModel))>可能属于类HostModel。而不是实现你自己的单例,你应该用<PartCreationPolicy(CreationPolicy.Shared)>.

然后就是你有一个 import on ImportData,但是那个类也没有被导出,你只是用一个new语句自己创建它。因此,MEF 永远不会看到这些实例,也不会对导入做任何事情。

最后,请注意 ASP.NET MVC 不会自动使用 MEF 创建对象。您需要注册一个包含 MEF 容器的依赖解析器来进行设置。MEF2 中似乎有APS.NET MVC 的组合提供程序。

于 2012-06-21T14:47:40.250 回答