简体   繁体   English

剃刀类库:在分页符中引用其他程序集Azure发布

[英]Razor Class Library: referencing other assembly in Page breaks Azure publish

Starting point : the Razor Class Library (RCL) tutorial . 起点: Razor类库(RCL)教程 I can publish it on Azure, and access the Razor page as expected. 我可以在Azure上发布它,并按预期访问Razor页面。

Now, I'm creating another .netstandard library, called MyCompany.ClassLibrary1 . 现在,我正在创建另一个.netstandard库,称为MyCompany.ClassLibrary1 It contains a single class: 它包含一个类:

namespace MyCompany.ClassLibrary1
{
    public class Class1
    {
        public string Message => "This is a test";
    }
}

I'm referencing it in the Page1Model class, like this: 我在Page1Model类中引用它,如下所示:

using Microsoft.AspNetCore.Mvc.RazorPages;
using MyCompany.ClassLibrary1;

namespace RazorClassLib.MyFeature.Pages
{
    public class Page1Model : PageModel
    {
        public string Message;

        public void OnGet()
        {
            Message = new Class1().Message;
        }
    }
}

Finally, just to check that everything works properly, I use it in Page1.cshtml : 最后,为了检查一切是否正常,我在Page1.cshtml使用了它:

@page
@model RazorClassLib.MyFeature.Pages.Page1Model

<html>
<body>
    OK from Razor page lib: @Model.Message
</body>
</html>

When launching on my machine, everything works perfectly. 在我的计算机上启动时,一切正常。

However, when trying to publish the web app on Azure, the Razor page precompilation fails, with the following error: 但是,当尝试在Azure上发布Web应用程序时,Razor页面的预编译失败,并出现以下错误:

Areas\\MyFeature\\Pages\\Page1.cshtml.cs(2,7): Error CS0246: The type or namespace name 'MyCompany' could not be found (are you missing a using directive or an assembly reference?)

Questions : 问题

  1. Why does the publish step tries to recompile the Razor pages from the RCL? 为什么发布步骤尝试从RCL重新编译Razor页面?
  2. Why can't it find the namespace from my library? 为什么无法从我的库中找到名称空间?

I've created a small GitHub repository with all the details. 我创建了一个包含所有详细信息的小型GitHub存储库。

The fix was simply to add a reference to the proper Razor SDK to the RCL project. 解决方法只是将对正确的Razor SDK的引用添加到RCL项目中。 For some reason, it was using an old version. 由于某种原因,它使用的是旧版本。

Fix : add a reference to Nuget package Microsoft.NET.Sdk.Razor version 2.1.1 . 修复:添加对Nuget包Microsoft.NET.Sdk.Razor 2.1.1版的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM