开发者

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:

  • System
  • System.Web
  • System.Core

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开发者_如何学C: 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!


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?


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);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜