I am trying to share a cshtml file between a MVC Framework project and a MVC Core project, hence this requirement to hide the using and inherits directives from another.
(The following uses references to the using
dire
In short, a cshtml
file can be shared only if C# code within the file uses: namespace/classes defined in both the .NET Framework and .NET Core framework (and use the same method signatures producing the expected output in both frameworks); or namespace/class definitions imported into both projects that are independent of the framework (like your own namespace/class definitions).
Even if there's a way to "hide" the using
directive from a framework, C# code in a Razor page could still be referencing namespaces/classes that are likely defined/shipped only in .NET Framework or .NET Core.
You could eliminate the using
directive altogether and just use fully qualified namespace types, but C# Intellisense will show a red squiggly underline for types if a Razor page is used in a project where the project's framework doesn't ship with the namespace package/library/file.
C# preprocessor directives
The full list of preprocessor directives for C# is listed here. There isn't a directive listed that identifies a way to hide a using
directive (i.e. not have the directive included based on the project type).
'Mvc' does not exist in the namespace 'System.Web'
When I do the above, compiler seems to simply ignore the #ifs and is giving me this error, when compiling in a Core project
The System.Web.Mvc
namespace is part of the .NET Framework but not part of .NET Core. This is why the error you listed, "The type or namespace name 'Mvc' does not exist in the namespace 'System.Web'", results when the using
directive for System.Web.Mvc.Html
occurs in the .NET Core project.
Microsoft's documentation for the System.Web
namespace in .NET Framework 4.8 lists the following:
For .NET Core and .NET 5+, this namespace contains the HttpUtility class.
For .NET Framework, this namespace contains classes and interfaces that enable browser-server communication. These classes include the HttpRequest class, which provides extensive information about the current HTTP request; the HttpResponse class, which manages HTTP output to the client; and the HttpServerUtility class, which provides access to server-side utilities and processes. System.Web also includes classes for cookie manipulation, file transfer, exception information, and output cache control in .NET Framework.
using
directive
For clarity in reference to a using
directive versus a using
statement
The purpose of a using
directive, as defined in the documentation, is to:
use types defined in a namespace without specifying the fully qualified namespace of that type. In its basic form, the using directive imports all the types from a single namespace.
Create a using
directive to use the types in a namespace without having to specify the namespace.
using System.Text
This reference provides a clearer definition:
Fully qualified namespace names can become quite long and unwieldy. It is possible, however, to import all the types from one or more namespaces into a file so that they can be used without full qualification. To achieve this, the C# programmer includes a using directive, generally at the top of the file.
The same Microsoft documentation adds the following as a side note:
The using
keyword is also used to create using statements, which help ensure that IDisposable objects such as files and fonts are handled correctly
using (var reader = new StringReader(manyLines))
{
}
Conditional compilation
The C# language specification for conditional compilation directives says
#if, #elif, #else, and #endif, which are used to skip conditionally sections of source code (ยง6.5.5)
Is the using
directive considered source code?
The comment from @Michael Petch in this Stack Overflow post adds to understanding the using
directive:
directives are instructions for the assembler that may emit instructions that get assembled/linked and then executed at runtime.
A using
directive is used by C# Intellisense when writing code (at design time) and later by they compiler.