How to set the default namespace in projects using project.json (.xproj)
Asked Answered
D

2

23

In a standard .csproj you could go into properties and set the default namespace. How can this be achieved in a .xproj project using project.json?

Deafening answered 24/2, 2016 at 12:28 Comment(3)
AFAIK this can't be done with a project.json. You can do it with an xproj the same way you used to do it with a csproj though. Right click it in Visual Studio, and on the Application tab, change the Default namespace.Naturally
Add that as an answer, so I can mark it so.Deafening
you can do it via project.json, see my answer below.Volkslied
N
9

AFAIK this can't be done with a project.json. You can do it with an xproj the same way you used to do it with a csproj though. Right click it in Visual Studio, and on the Application tab, change the Default namespace.

Naturally answered 24/2, 2016 at 13:54 Comment(0)
V
44

With ASP.NET Core 1.0.1, you can set your default namespace in the project.json file as follows:

"tooling": {
   "defaultNamespace": "Your.Name.Space"
}

The yeoman ASP.NET generator will respect this defaultNamespace when generating new classes.

For the new Visual Studio 2017 csproj tooling, you can add the following XML to change your default namespace (up in the top level <PropertyGroup> reference):

<PropertyGroup>
  <Optimize>true</Optimize>
  ...
  <RootNamespace>My.Root.Namespace</RootNamespace>
</PropertyGroup>

This is only necessary if your .csproj filename does not match your intended root namespace for the project.

Volkslied answered 24/10, 2016 at 11:49 Comment(5)
Where is the "tooling" element supposed to go in project.json?Moreau
It goes into the root of project json, so on the same level as dependencies.Volkslied
I can't find the project.json file within my mvc projectGarry
@muhammadtayyab that's because all the new tooling from Microsoft ditched the project.json in favor of csproj files - so read the last part of my post to figure out what to do :)Volkslied
Yes, I was thinking for the same in .Net Core we only have .csproj fileGarry
N
9

AFAIK this can't be done with a project.json. You can do it with an xproj the same way you used to do it with a csproj though. Right click it in Visual Studio, and on the Application tab, change the Default namespace.

Naturally answered 24/2, 2016 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.