1

In my .NET MVC project I have a model called User, whoose class is conflicting with System.Security.Principal.IPrincipal.WebPageRenderingBase.User when I'm inside a razor view. (and only inside a razor view).

Is there a way to give my class a different global alias? or better yet, stop this .NET class from showing up?

I don't want a verbose solution. (prefixing) is there a way to remvoe reference to this stupid User property?

Is this a common problem for everyone or is there something wrong with my setup?

williamsandonz
  • 15,864
  • 23
  • 100
  • 186

5 Answers5

12

Just rename your model class from User to Person. Every solution will bloat your code with unnecesary complexity.

Other thing: If you are using a class named User 99% certain that you are using your models in views, where the best approach is using a ViewModel, where you collect all the data you need to pass to your view. So, in a view, the User would became one property of this ViewModel.

This approach also help you in eradicate the use of ViewBags and similar...

Romias
  • 13,783
  • 7
  • 56
  • 85
  • Ah I'm using Viewmodels in views thanks tho! :-) I just don't create a Viewmodel for a view if it doesn't need it. – williamsandonz Jun 25 '13 at 03:57
  • Thank you also for the person recommendaion! I may look at doing this, of course it means I'd have to change the routes from person -> user which is a bit annoying. Hmph!!, why on earth is there a property called User exposed in razor views, seems silly to me! – williamsandonz Jun 25 '13 at 03:59
  • I've got to agree with renaming your model. If you can avoid name clashes with parts of the .net framework, then do it. If it's really not possible to rename, then take a look at the answer further down about fully qualifying the class - it's not pretty, but it will work – dave clements Jun 25 '13 at 14:27
4

Fully qualify your class i.e.

MyNamespace.Core.User

as opposed to just

User

Although I'm a little confused because

System.Security.Principal.IPrincipal.WebPageRenderingBase.User

Is a property of type IPrincipal not a type called User

FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55
  • 3
    Also, if you need a particular class in just one file, you can use this at the top: `using ClassName = Fully.Qualified.Namespace.ClassName;` and it will assume for the duration of that file that unqualified references to `ClassName` are for the class you specified. – Katie Kilian Feb 22 '13 at 21:31
  • :O that's super neat! I had no idea! – FlyingStreudel Feb 22 '13 at 21:32
  • Perhaps it's a conflict with the `User` property for code that executes in a class derived from `WebPageRenderingBase` – CodesInChaos Feb 22 '13 at 21:40
  • 1
    I thought that as well, but I'm fairly sure I can declare a `User u = new User();` inside a class with a `this.User` property without any compilation errors. You'll probably get yelled at if you try to call a static method on the similarly named class though. – FlyingStreudel Feb 22 '13 at 21:44
  • I'd like a global solution if possible, and one without a verbose namespace prefixing everytime. sigh annoying huh! – williamsandonz Feb 22 '13 at 22:51
4

I typically do something like the following:

using m = MyProject.MyNameSpace.MySubNameSpace;

var u = new m.User();
devlife
  • 15,275
  • 27
  • 77
  • 131
4

This is somewhat of a mix between @devlife and @Romias answers.

By implementing a using alias you can "rename" (in the file this is defined in) your User class to something you specify in the using statements.

For example:

using Person = NameSpace.SubNameSpace.Classes.User

Then you can use Person as your User class within the file that this alias is defined in.

See: http://msdn.microsoft.com/en-us/library/sf0df423(v=vs.71).aspx

The last section provides a full code example of this.

Samuel Parkinson
  • 2,992
  • 1
  • 27
  • 38
1

Perhaps the great Jon Skeet has your answer:

"What use is the Aliases property of assembly references in Visual Studio 8":

This is for "extern aliases". Suppose you want to use two different types, both of which are called Foo.Bar (i.e. Bar in a namespace of Foo). The two types will be in different assemblies (by definition) - you use the property in VS to associate an alias with each reference, then you can do:

extern alias FirstAlias;
extern alias SecondAlias;

using FirstBar = FirstAlias::Foo.Bar;
using SecondBar = SecondAlias::Foo.Bar;

and then use FirstBar and SecondBar in your code.

So basically it's an extra level of naming - and you shouldn't use it unless you really, really have to. It will confuse a lot of people. Try to avoid getting into that situation in the first place - but be aware of this solution for those times where you just can't avoid it.

Community
  • 1
  • 1
antiduh
  • 11,853
  • 4
  • 43
  • 66
  • 2
    This user does not have that problem. They have a class named 'User' conflicting with a *member* named user, not another class. – Andrew Barber Jun 25 '13 at 01:45
  • Hrm I guess. Never used Razor Views, but I would've figured this is exactly what he was looking for. Thanks. – antiduh Jun 25 '13 at 01:52
  • Had nothing to do with Razor; what you posted is for dealing with the classes of the same name and namespace. That's not the OP's problem at all. – Andrew Barber Jun 25 '13 at 01:54