Search code examples
asp.net-mvcmef

ASP.NET MVC With MEF integration


I am following the same approach used in this link in my ASP.NET MVC 2 project and it is working fine.

All my pages are partial views (ascx). So I have created a class library in my solution as a plugin the same way done in the link sample and put a new partial view and a controller that is called from the host application using MEF the same way also and it worked fine.

The difference in my plugin project is that I created a new folder to put my models in.

This code below represent the partial view showing as a plugin view in my host application through MEF successfully

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div class="Container">
<p>Hello I am a Plugin Partial View</p>
</div>

But since I have added a model in my application models folder, I need to bind it to the partial view the same way done in my host ASP.NET MVC 2 host project.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Plugin.Models.ViewModel>" %>
<div class="Container">
<p>Hello I am a Plugin Partial View</p>
</div>

Once adding the ViewModel to the partial view an error shows in firebug and my view is not loading anymore. The error in http response showing in firebug looks as follows:

<!DOCTYPE html>
<html>
<head>
<title>Parser Error</title>
<meta name="viewport" content="width=device-width" />
<style>
 body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
 p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
 b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
 H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
 H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
 pre {font-family:"Consolas","Lucida Console",Monospace;font-    size:11pt;margin:0;padding:0.5em;line-height:14pt}
 .marker {font-weight: bold; color: black;text-decoration: none;}
 .version {color: gray;}
 .error {margin-bottom: 10px;}
 .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
 @media screen and (max-width: 639px) {
  pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
 }
 @media screen and (max-width: 479px) {
  pre { width: 280px; }
 }
</style>
</head>

<body bgcolor="white">

        <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>

        <h2> <i>Parser Error</i> </h2></span>

        <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

        <b> Description: </b>An error occurred during the parsing of a resource required to service this request.   Please review the following specific parse error details and modify your source file appropriately.
        <br><br>

        <b> Parser Error Message: </b>Could not load type 'System.Web.Mvc.ViewUserControl&lt;Plugin.Models.ViewModel&gt;'.<br><br>

        <b>Source Error:</b> <br><br>

        <table width=100% bgcolor="#ffffcc">
           <tr>
              <td>
                  <code><pre>

Line 11: --%&gt;
Line 12: 
<font color=red>Line 13: &lt;%@ Control Language=&quot;C#&quot;     Inherits=&quot;System.Web.Mvc.ViewUserControl&lt;Plugin.Models.ViewModel&gt;&quot; %&gt;
</font>Line 14: 
Line 15: </pre></code>

              </td>
           </tr>
        </table>

        <br>

        <b> Source File: </b> /Modules/Message/Views/Message/PartialView.ascx<b> &nbsp;&nbsp; Line: </b> 13
        <br><br>

        <hr width=100% size=1 color=silver>

        <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18010

        </font>

</body>
</html>
<!-- 
[HttpException]: Could not load type     &#39;System.Web.Mvc.ViewUserControl&lt;Plugin.Models.ViewModel&gt;&#39;.
at System.Web.UI.TemplateParser.GetType(String typeName, Boolean ignoreCase, Boolean    throwOnError)
at System.Web.UI.TemplateParser.ProcessInheritsAttribute(String baseTypeName, String codeFileBaseTypeName, String src, Assembly assembly)
at System.Web.UI.TemplateParser.PostProcessMainDirectiveAttributes(IDictionary  parseData)
[HttpParseException]: Could not load type    &#39;System.Web.Mvc.ViewUserControl&lt;Plugin.Models.ViewModel&gt;&#39;.
at System.Web.UI.TemplateParser.ProcessException(Exception ex)
at System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath,   Encoding fileEncoding)
[HttpParseException]: Could not load type    &#39;System.Web.Mvc.ViewUserControl&lt;Plugin.Models.ViewModel&gt;&#39;.
at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath,    Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseFile(String physicalPath, VirtualPath virtualPath)
at System.Web.UI.TemplateParser.Parse()
at System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType()
at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider)
at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath     virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile,    Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext   context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean  allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath  virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp)
at System.Web.Mvc.WebFormViewEngine.FileExists(ControllerContext controllerContext, String virtualPath)
at   System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromSpecificName(ControllerContext  controllerContext, String name, String cacheKey, String[]& searchedLocations)
at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext  controllerContext, String[] locations, String[] areaLocations, String  locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean  useCache, String[]& searchedLocations)
at System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache)
at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClass6.<FindView>b__5(IViewEngine e)
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)
at System.Web.Mvc.ViewResult.FindView(ControllerContext context)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.  <InvokeActionResultWithFilters>b__11()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter  filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter  filter, ResultExecutingContext preContext, Func`1 continuation)
at    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext  controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at  System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&   completedSynchronously)
-->

I have also copied the web.config file and added the model namespace as following:

<system.web>
  <pages>
    <namespaces>
      <add namespace='Plugin.Models.ViewModel' />
    </namespaces>
  </pages>
</system.web>

But this is not working either.


Solution

  • See Tom Clarkson answer on

    Views in separate assemblies in ASP.NET MVC