Razor page namespace conflicts with class library - c#

I have a razor page with a namespace like MyApp.Pages.MyClass. Within this page cshtml I am trying to reference an enum in a class library with a namespace like MyClass.Enums.MyEnum. I can't find a way to reference this enum type as using the fully qualified namespace (or trying to add a using clause) both result in a "Type or namespace does not exist" error because it is treating MyClass.Enums.MyEnum as relative to the current namespace MyApp.Pages.MyClass. Is there a way I can reference the enum type without renaming the namespaces involved?

I have a razor page with a namespace like MyApp.Pages.MyClass. Within this page cshtml I am trying to reference an enum in a class library with a namespace like MyClass.Enums.MyEnum.
result in a "Type or namespace does not exist" error
Can reproduce same issue if we name the page model class as MyClass, not MyClassModel.
namespace MyApp
{
public class MyClass : PageModel
In MyClass.cshtml page and error as below.
To fix it, you can rename your page model class, such as MyClassModel etc.
You can also try another approach to create and use a nested namespace, like below.
namespace MyApp
{
namespace MyAppNestedNamespace
{
public class MyClass : PageModel
{
public void OnGet()
{
//...
}
//...
}
}
}
The MyClass.cshtml page may look like this:
#page
#model MyAppNestedNamespace.MyClass
#using MyClass.Enums
#{
ViewData["Title"] = "MyClass";
}
<h1>MyClass</h1>
#*your code here*#

Just dereference to the global namespace:
using MyEnum = global::MyClass.Enums.MyEnum

Related

Unable to run xaml and c# example [duplicate]

My program uses a class called Time2. I have the reference added to TimeTest but I keep getting the error, 'Time2' is a 'namespace' but is used like a 'type'.
Could someone please tell me what this error is and how to fix it?
namespace TimeTest
{
class TimeTest
{
static void Main(string[] args)
{
Time2 t1 = new Time2();
}
}
}
I suspect you've got the same problem at least twice.
Here:
namespace TimeTest
{
class TimeTest
{
}
... you're declaring a type with the same name as the namespace it's in. Don't do that.
Now you apparently have the same problem with Time2. I suspect if you add:
using Time2;
to your list of using directives, your code will compile. But please, please, please fix the bigger problem: the problematic choice of names. (Follow the link above to find out more details of why it's a bad idea.)
(Additionally, unless you're really interested in writing time-based types, I'd advise you not to do so... and I say that as someone who does do exactly that. Use the built-in capabilities, or a third party library such as, um, mine. Working with dates and times correctly is surprisingly hairy. :)
namespace TestApplication // Remove .Controller
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Remove the controller word from namepsace
The class TimeTest is conflicting with namespace TimeTest.
If you can't change the namespace and the class name:
Create an alias for the class type.
using TimeTest_t = TimeTest.TimeTest;
TimeTest_t s = new TimeTest_t();
All the answers indicate the cause, but sometimes the bigger problem is identifying all the places that define an improper namespace. With tools like Resharper that automatically adjust the namespace using the folder structure, it is rather easy to encounter this issue.
You can get all the lines that create the issue by searching in project / solution using the following regex:
namespace .+\.TheNameUsedAsBothNamespaceAndType
If you're working on a big app and can't change any names, you can type a . to select the type you want from the namespace:
namespace Company.Core.Context{
public partial class Context : Database Context {
...
}
}
...
using Company.Core.Context;
someFunction(){
var c = new Context.Context();
}
I had this problem as I created a class "Response.cs" inside a folder named "Response". So VS was catching the new Response () as Folder/namespace.
So I changed the class name to StatusResponse.cs and called new StatusResponse().This solved the issue.
If you are here for EF Core related issues, here's the tip:
Name your Migration's subfolder differently than the Database Context's name.
This will solve it for you.
My error was something like this:
ModelSnapshot.cs error CS0118: Context is a namespace but is used like a type
Please check that your class and namespace name is the same...
It happens when the namespace and class name are the same.
do one thing write the full name of the namespace when you want to use the namespace.
using Student.Models.Db;
namespace Student.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<Student> student = null;
return View();
}
}
if the error is
Line 26:
Line 27: #foreach (Customers customer in Model)
Line 28: {
Line 29:
give the full name space
like
#foreach (Start.Models.customer customer in Model)

mvvm cross Partial Declarations must not specify different base classes

I have some trouble with the MVVM cross declarations. Everytime i change the view from ContentPage to MvxContentPage it shows this declaration error.
public partial class TestView: ContentPage
{
public TestView()
{
InitializeComponent();
BindingContext = new TestViewModel();
}
}
to this;
public partial class TestView: MvxContentPage<TestViewModel>
{
public TestView()
{
InitializeComponent();
}
}
XAML;
<views:MvxContentPage xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
it shows me "Partial Declarations of TestView must not specify different base classes". I already tried all solutions from preview post.
Make sure that you have specified the correct type argument and that you don't have any other class with the same name:
<views:MvxContentPage x:TypeArguments="viewModels:TestViewModel"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:YourProject.ViewModels;assembly=YourProject"
xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
x:Class="YourProject.Forms.UI.Pages.TestView">
...
It sounds like you have another file somewhere in your project that has the line
public partial class TestView : ContentPage
It could be in some other folder, but still have the same namespace (particularly if you've been doing cuts-and-pastes of files).

The tag "XXX" does not exist in xml namespace

My SolutionName: TestBA
My AssemblyName: TestBA
Default namespace: Examples.Bootstrapper
In TestBA assemble I have Converters folder:
namespace Examples.Bootstrapper.Converters
{
class PercentToPixelsConverter : IValueConverter
(...)
I trying to use:
xmlns:c="clr-namespace:Examples.Bootstrapper.Converters;assembly=TestBA"
(...)
<c:PercentToPixelsConverter x:Key="MyName" />
I get an error:
The tag 'PercentToPixelsConverter' does not exist in XML namespace
'clr-namespace:Converters;assembly=TestBA'.
You will need to make your class public:
public class PercentToPixelsConverter
You don't have to specify assembly name if the namespace is from the same project or assembly :
xmlns:c="clr-namespace:Examples.Bootstrapper.Converters"

'namespace' but is used like a 'type'

My program uses a class called Time2. I have the reference added to TimeTest but I keep getting the error, 'Time2' is a 'namespace' but is used like a 'type'.
Could someone please tell me what this error is and how to fix it?
namespace TimeTest
{
class TimeTest
{
static void Main(string[] args)
{
Time2 t1 = new Time2();
}
}
}
I suspect you've got the same problem at least twice.
Here:
namespace TimeTest
{
class TimeTest
{
}
... you're declaring a type with the same name as the namespace it's in. Don't do that.
Now you apparently have the same problem with Time2. I suspect if you add:
using Time2;
to your list of using directives, your code will compile. But please, please, please fix the bigger problem: the problematic choice of names. (Follow the link above to find out more details of why it's a bad idea.)
(Additionally, unless you're really interested in writing time-based types, I'd advise you not to do so... and I say that as someone who does do exactly that. Use the built-in capabilities, or a third party library such as, um, mine. Working with dates and times correctly is surprisingly hairy. :)
namespace TestApplication // Remove .Controller
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Remove the controller word from namepsace
The class TimeTest is conflicting with namespace TimeTest.
If you can't change the namespace and the class name:
Create an alias for the class type.
using TimeTest_t = TimeTest.TimeTest;
TimeTest_t s = new TimeTest_t();
All the answers indicate the cause, but sometimes the bigger problem is identifying all the places that define an improper namespace. With tools like Resharper that automatically adjust the namespace using the folder structure, it is rather easy to encounter this issue.
You can get all the lines that create the issue by searching in project / solution using the following regex:
namespace .+\.TheNameUsedAsBothNamespaceAndType
If you're working on a big app and can't change any names, you can type a . to select the type you want from the namespace:
namespace Company.Core.Context{
public partial class Context : Database Context {
...
}
}
...
using Company.Core.Context;
someFunction(){
var c = new Context.Context();
}
I had this problem as I created a class "Response.cs" inside a folder named "Response". So VS was catching the new Response () as Folder/namespace.
So I changed the class name to StatusResponse.cs and called new StatusResponse().This solved the issue.
If you are here for EF Core related issues, here's the tip:
Name your Migration's subfolder differently than the Database Context's name.
This will solve it for you.
My error was something like this:
ModelSnapshot.cs error CS0118: Context is a namespace but is used like a type
Please check that your class and namespace name is the same...
It happens when the namespace and class name are the same.
do one thing write the full name of the namespace when you want to use the namespace.
using Student.Models.Db;
namespace Student.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<Student> student = null;
return View();
}
}
if the error is
Line 26:
Line 27: #foreach (Customers customer in Model)
Line 28: {
Line 29:
give the full name space
like
#foreach (Start.Models.customer customer in Model)

Why a class in App_Code/BLL is not accessible in pages?

Hi I have a class in folder App_Code/BLL/Countries.cs ,which is a public class.
But when I tried to access the class in a ASP.NET page like:
Countries MyCountries = new Countries();
its showing error:
"The type or namespace name 'Countries' could not be found (are you missing a using directive or an assembly reference?)"
Ok your countries class will look something like this
namespace BGridNDynamicPage1
{
public class Countries()
{
......
}
}
and your code above will be something like this:
using System.Web;
etc.
namespace BGridNDynamicPage
{
public class something() : Page
{
Countries MyCountries = new Countries();
}
}
change your second class too:
using BGridNDynamicPage1;
using System.Web;
etc.
namespace BGridNDynamicPage
{
public class something() : Page
{
Countries MyCountries = new Countries();
}
}
See http://msdn.microsoft.com/en-us/library/sf0df423.aspx

Categories

Resources