简体   繁体   中英

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

Starting point : the Razor Class Library (RCL) tutorial . I can publish it on Azure, and access the Razor page as expected.

Now, I'm creating another .netstandard library, called 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:

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 :

@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:

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?
  2. Why can't it find the namespace from my library?

I've created a small GitHub repository with all the details.

The fix was simply to add a reference to the proper Razor SDK to the RCL project. For some reason, it was using an old version.

Fix : add a reference to Nuget package Microsoft.NET.Sdk.Razor version 2.1.1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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