Kasbolat Kumakhov
Kasbolat Kumakhov

Reputation: 721

Referencing a class from another project in a RazorPage

I have a ASP .Net Core 3.1 project named "MyProject.app", using Razor Pages. This project references another one (in the same solution) that is named "Utils.lib". The latter contains this class:

namespace Utils.lib
{
    public static class PropertyUtils
    {
        public static void SomeMethod()
        {
        }
    }
}

Now in a "MyProject.app" i have a razor page that is located under:

/Pages/Profile/Index.cshtml

And i'm trying to reference my util "Utils.lib" class there like this:

Utils.lib.PropertyUtils.SomeMethod()

But all i get is

The type or namespace name 'lib' does not exist in the namespace 'MyProject.app.Utils' (are you missing an assembly reference?)

I tried to add a namespace to _ViewImports.cshtml, with a @using directive but it gives the same error.

At the same time, i can freely use that method in any of my .cs files of the main project, but not in any .cshtml

Also i have this class in main project:

namespace MyProject.app.Utils
{
    class SomeOtherUtilClass
    {
    }
}

My guess is that it has something to do with the way i named my namespaces, but i can't figure out what.

What am i missing here? Why can't i reference my classes in RazorPages?

Upvotes: 2

Views: 2138

Answers (1)

Anton
Anton

Reputation: 841

So, here is my answer. You will be able to reproduce the problem even in your c# code if you add these usings:

using MyProject.app
using Utils.lib

Because MyProject.app contains the same namespace, the compiler cannot find your Utils.lib.

You have two ways of solving this problem:

  1. Rename namespaces
  2. Use in your razor pages full namespace paths like:
@MyProject.app.Utils.SomeUtilClass

or

@Utils.lib.SomeUtilClass

So, you have to remove @using MyProject.app from _ViewImports

Upvotes: 1

Related Questions