2017-10-10 6 views
0

別のアセンブリでマイプロジェクトカミソリページを探したい。 これを行うために、私は次のコード書き込み:他のアセンブリでカミソリページを見つける

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.

スタック:

を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;

とはfalseにPreserveCompilationContextを設定したが、今、私はこの問題を解決することができますどのように働いていましたか?

答えて

-1

は、あなたがWebApplication5のあなたの_ViewImports.cshtmlで@using文が必要ですか?代わりにNEWINGのAssemblyNameのApp.SameTodoModel

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")); 
    }); 
} 
関連する問題