2014-12-07 145 views
4

在MVC5中,通过使用Razor Generator(http://razorgenerator.codeplex.com)这样的工具,可以在项目之间共享视图(Razor)。 如何在vNext中实现相同?我的视图无法识别(包含视图的项目在project.json中被列为依赖项)。如何在ASP.NET vNext MVC 6(beta1)项目之间共享视图?

InvalidOperationException: The partial view '~/Views/Authentication/_LogInForm.cshtml' was not found. The following locations were searched: ~/Views/Authentication/_LogInForm.cshtml

+0

我应该提到它:在源项目中包含“RazorGenerator.Mvc”不再有效(参考在它前面加上“!”)。 – Vincent 2014-12-07 15:08:43

回答

3

我们终于设法解决了这个问题。虽然不太容易...

  1. 您需要将视图作为资源嵌入到您将依赖的项目中。为此,请将"resources": [ "**/*.cshtml" ]添加到其project.json中。

  2. 您需要创建一个可以查看这些资源而不是在磁盘上查找的IFileSystem。这是棘手的部分。我把这个pastbin为lisibility:http://pastebin.com/aNfq5hNi

  3. 你需要在你的Startup.cs注册此IFileSystem:

//... public void Configure(IApplicationBuilder app,IHostingEnvironment env,ILoggerFactory loggerfactory) { //Enable use of views in other assemblies IOptions<RazorViewEngineOptions> razorViewEngineOptions=app.ApplicationServices.GetService<IOptions<RazorViewEngineOptions>>(); razorViewEngineOptions.Options.FileSystem=new MVCAsset.EmbeddedExpiringFileInfoCache( razorViewEngineOptions, app.ApplicationServices.GetService<ILibraryManager>() ); //... } //...

注:这是实际测试和MVC6 RC1工作,我没有测试BETA1。

+0

您是否更新过此代码以使用MVC6-beta4?由于库中的更改发生变化,您发布的部分代码不再适用。 – 2015-05-13 22:58:14

+0

你有一个测试版5? – 2015-07-15 11:51:37

+0

任何人都看着这个更新的测试版6,7等? – 2015-10-09 19:53:23

2

我不知道如果这有助于外部程序访问的观点,但添加这些观点可以发现,你可以实现IViewLocationExpander这样的位置:

public class ViewLocationExpander : IViewLocationExpander 
{ 
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) 
    { 
     var locations = new List<string>(viewLocations); 

     locations.Add("Views/MyOtherViewLocation/{0}.cshtml"); 

     return locations; 
    } 

    public void PopulateValues(ViewLocationExpanderContext context) 
    { 

    } 
} 

在启动。 cs ConfigureServices方法,加:

 services.Configure<RazorViewEngineOptions>(options => 
     { 
      var expander = new ViewLocationExpander(); 
      options.ViewLocationExpanders.Add(expander); 
     }); 
+0

感谢您的回答,即使它没有回答问题。我上面发布了一个有关我自己问题的工作答案。 – Vincent 2015-01-05 16:16:13

+0

是的,您无法使用此解决方案使用web根外部的自定义视图位置。 – whyleee 2015-06-18 20:13:44