2017-10-10 10 views
0

다른 어셈블리에서 내 프로젝트 레이저 포인터 페이지를 찾고 싶습니다. 이 일을 위해 나는 다음 코드 쓰기 :다른 어셈블리에서 Razor 페이지 찾기

public void ConfigureServices(IServiceCollection services) 
{ 
    var adminAssembly = Assembly.Load(new AssemblyName("App")); 
    services.AddMvc().AddApplicationPart(adminAssembly).AddRazorOptions(options => 
    { 
     var previous = options.CompilationCallback; 
     options.CompilationCallback = context => 
     { 
      previous?.Invoke(context); 

      context.Compilation = context.Compilation.AddReferences(
       MetadataReference.CreateFromFile(typeof(dodo).Assembly.Location)); 
     }; 
    }); 

    services.Configure<RazorViewEngineOptions>(options => 
    { 
     options.FileProviders.Add(new EmbeddedFileProvider(Assembly.Load("App"))); 
     options.FileProviders.Add(new PhysicalFileProvider(@"C:\Users\soheil\Documents\Visual Studio 2017\Projects\WebApplication5\App")); 
    }); 
} 

내 솔루션 :

devenv_2017-10-10_18-44-26

다음과 같은 오류 받기 localhost:5000/SameTodo을 실행 :

One or more compilation references are missing. Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false.

스택 :

,536,913,632을 10

The type or namespace name 'SameTodoModel' could not be found (are you missing a using directive or an assembly reference?) + public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } The type or namespace name 'SameTodoModel' could not be found (are you missing a using directive or an assembly reference?) + public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary)PageContext?.ViewData; The type or namespace name 'SameTodoModel' could not be found (are you missing a using directive or an assembly reference?) + public SameTodoModel Model => ViewData.Model; The type or namespace name 'SameTodoModel' could not be found (are you missing a using directive or an assembly reference?) + public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary)PageContext?.ViewData;

으로 설정하고 PreserveCompilationContext을 false로 설정하면 어떻게 작동합니까?이 문제를 해결하려면 어떻게해야합니까?

답변

-1

WebApplication5의 _ViewImports.cshtml에 @using 문이 필요합니까? App.SameTodoModel

대신 newing의의 AssemblyName의
0

을 @using 마찬가지로, 해당 어셈블리에서 유형의 참조를 사용

var adminAssembly = typeof(SameTodoModel).Assembly; 또한

는 오류 메시지가 말한다 : 'PreserveCompilationContext'속성이 false로 설정되어 있지 .

의미는 true로 설정해야합니다.

+0

나는 이것을하지만 작동하지 않습니다. –

+0

어떻게 든 솔루션을 공유해 주실 수 있습니까? Github에 게시하는 것과 같은가? 면도기 뷰에서 사용하고있는 클래스의 네임 스페이스에'using' 지시어가 없을 수도 있습니까? –

2
public void ConfigureServices(IServiceCollection services) 
{ 
    var adminAssembly = Assembly.Load(new AssemblyName("App")); 
    services.AddMvc().AddApplicationPart(adminAssembly).AddRazorOptions(options => 
    { 
     var previous = options.CompilationCallback; 
     options.CompilationCallback = context => 
     { 
      previous?.Invoke(context); 

      var referenceAssemblies = AppDomain.CurrentDomain.GetAssemblies() 
       .Where(x => !x.IsDynamic&& !string.IsNullOrEmpty(x.Location)) 
       .Select(x => MetadataReference.CreateFromFile(x.Location)) 
       .ToList(); 

      //add dynamic 
      var dynamicAssembly = typeof(DynamicAttribute).Assembly; 
       referenceAssemblies.Add(MetadataReference.CreateFromFile(dynamicAssembly.Location)); 

      context.Compilation = context.Compilation.AddReferences(referenceAssemblies); 
     }; 
    }); 

    services.Configure<RazorViewEngineOptions>(options => 
    { 
     options.FileProviders.Add(new EmbeddedFileProvider(Assembly.Load("App"))); 
     options.FileProviders.Add(new PhysicalFileProvider(@"C:\Users\soheil\Documents\Visual Studio 2017\Projects\WebApplication5\App")); 
    }); 
}