Andrew Bullock
Andrew Bullock

Reputation: 37398

Referencing internal class in MVC View

If I reference an internal class from inside an MVC view, the AssemblyBuilder complains that it can't access the class (which makes sense) here:

System.Web.Compilation.AssemblyBuilder.Compile()

I've tried adding InternalsVisibleTo attributes for:

but I can't get it to work. Is this possible somehow?

Demo:

internal static class InternalClass
{
    public static string Foo = "bar";
}

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<%: InternalClass.Foo %>

Stack trace:

Compiler Error Message: CS0122: 'InternalClass' is inaccessible due to its protection level
at System.Web.Compilation.AssemblyBuilder.Compile()
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)

Edit: Please don't suggest alternative ways of doing it, that isn't what im asking.

Thanks!

Upvotes: 4

Views: 3312

Answers (3)

Herman Van Der Blom
Herman Van Der Blom

Reputation: 812

I needed also internal classes from Umbraco Assemblies and came up with this solution. Put this statement in the AssemblyInfo.cs from which you want to use Internal classes:

[assembly: InternalsVisibleTo("ActiveDirectory")]

Replace ActiveDirectory with your own Assembly. I wanted to use Internal classes from the Umbraco.Core assembly so I put this statement in the AssemblyInfo.cs from Umbraco.Core and it works like a charm.

Note! Do not forget to record all changes you made to Umbraco source code, so when there will be a new version you can bring in those changes you made.

Upvotes: 0

Victor Kornov
Victor Kornov

Reputation: 66

That's ASPX view engine, right? AFAIK it generates some code and dynamically compiles it into some randomly named assembly in Temporary Asp.Net Files folder. So, that means you are out of luck with InternalsVisibleTo attribute.

You may have some luck if you pre-compile your views e.g. Compile Views in ASP.NET MVC or Can I precompile my ASP.NET MVC application?

Upvotes: 3

archil
archil

Reputation: 39501

Views should use ViewModels. Why would you make some extra ninja level tricks to render the simple view :)? Define the view model

public class FooViewModel
{
     public string Foo {get;set;}
}

and in controller

public ActionResult FooAction
{
     FooViewModel model = new FooViewModel()
     {
          Foo = InternalClass.Foo
     };

     return View(model);
}

Upvotes: 2

Related Questions