Why do name / import spaces work differently between C # and Razor?

Employee, and I noticed something strange with Razor and imported namespaces.

Here is our test class, which we will try to get both from the C # file and from the Razor view.

namespace test
{
    public class c1 {}
}

namespace test.sub
{
    public class c2 {}
}

This is a sample of our C # code.

using test;

namespace test
{
    public class testbed
    {
        testbed()
        {
            c1 o1 = new c1();                   //works
            test.sub.c2 o2 = new test.sub.c2(); //works
            sub.c2 o3 = new sub.c2();           //works
        }
    }
}

This is a sample of our Razor code. "under" namespace is not available.

@using test
@(new c1())          @* Works *@
@(new test.sub.c2()) @* Works *@
@(new sub.c2())      @* Cannot be seen *@

Does anyone have an explanation why this works in our class files, but not in our views?

+4
source share
1 answer

sub.c2works in your code because you are in the namespace "test". Change the namespace to titi and you will see that it will not work.

namespace titi
{
    using test;

    public class testbed
    {
        testbed()
        {
            c1 o1 = new c1();                   //works
            test.sub.c2 o2 = new test.sub.c2(); //works
            sub.c2 o3 = new sub.c2();           //don't work
        }
    }
}

namespace test
{
    using test;

    public class testbed
    {
        testbed()
        {
            c1 o1 = new c1();                   //works
            test.sub.c2 o2 = new test.sub.c2(); //works
            sub.c2 o3 = new sub.c2();           //works because your are in namespace *test*
        }
    }
}

(, test), sub.c2 .

+5

All Articles