Scott Hanselman

Exploring dotnet new with .NET Core

July 18, 2016 Comment on this post [33] Posted in ASP.NET | ASP.NET MVC | DotNetCore | Open Source
Sponsored By

I'm very enjoying the "dotnet" command line. Mostly I do "dotnet new" and then add to the default Hello World app with the Visual Studio Code editor. Recently, though, I realized that the -t "type" and -l "lang" options are there and I wasn't using them. I think they are a little awkward, in that you have to:

dotnet new -t Web

when I think it should be more like dotnet new [type] as in

dotnet new web

What do you think? I find the -t a little heavy. I like the idea of "web" being empty, and "web/mvc" or "web/webapi" having more fleshed out stuff. Even "web/angular," you get the idea. Sound off in the comments. Regardless, there's cool templating tooling coming, I hear, but for now there's more there than I realized.

Of course, there's the default "dotnet new" which is a Hello World console app with a program.cs and project.json. In the future I think it will just run the app, and you'll have to do something like -v verbosity to get the details that we don't usually need to see.

C:\Users\scott\Desktop\test\console>dotnet run
Project console (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling console for .NETCoreApp,Version=v1.0

Compilation succeeded.
0 Warning(s)
0 Error(s)

Time elapsed 00:00:01.1591124

Hello World!

You can add -l (lang) to it and "dotnet new -l F#" and get an F# Console app rather than a C# one:

C:\Users\scott\Desktop\test\fsharp>dotnet run fabu!
Project fsharp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Hello World!
[|"fabu!"|]

C:\Users\scott\Desktop\test\fsharp>type Program.fs
// Learn more about F# at http://fsharp.org

open System

[<EntryPoint>]
let main argv =
printfn "Hello World!"
printfn "%A" argv
0 // return an integer exit code

There's also "dotnet new -t lib" which is super basic and gives you a quick new project with a Class1 and an Empty Method. Not so useful, but good to know.

You can also "dotnet new -t xunittest" to make a new test project. Nice that this is built-in! Now I just "dotnet test" after a "dotnet restore" and I get test results!

xUnit.net .NET CLI test runner (64-bit win10-x64)
Discovering: testing
Discovered: testing
Starting: testing
Finished: testing
=== TEST EXECUTION SUMMARY ===
testing Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.146s
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.

Side Note: If the folder name of the project is the same as one of the dependencies, it can confuse the resolver. For example, I did my new test project in a folder creatively named "XUnit." This is also the name of a dependency. I got the error: Errors in C:\Users\scott\Desktop\test\xunit\project.json Cycle detected:  xunit (>= 1.0.0) -> xunit (>= 2.1.0) -> xunit (>= 2.1.0). Note that 1.0.0 there. That's my project, which is 1.0.0. Solution? Rename my project's containing folder.

There's ASP.NET Core Hello World, which is "dotnet new -t Web." This will give you a nice simple ASP.NET Core app with some simple defaults that's setup for bower, gulp, and npm usage. I anticipate we'll see varying levels of what folks consider "complete."

yo aspnet: dotnet new -t web isn't the only way to make a new ASP.NET Core project from the command line (CLI). You can also use the Yeoman generator or "yo aspnet" to make very interesting projects, as well as create your own generators. In fact, Steve Sanderson has some impressive generators like his "aspnet-spa" generator for making Angular, React, and Knockout Single Page Apps (SPA) with ASP.NET Core.

image

All these generators work on Windows, Mac, and Linux, of course. I believe the intent is to reconcile them all such that Visual Studio proper and Visual Studio Code via the CLI will all get the same "File | New Project" results. Visual Studio will still be more "visual" but everything you can do in one world can and should be possible in another.


Sponsor: Big thanks to Redgate for sponsoring the feed this week. Have you got SQL fingers? Try SQL Prompt and you’ll be able to write, refactor, and reformat SQL effortlessly in SSMS and Visual Studio. Find out more!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
July 18, 2016 10:42
Enjoyed this post a lot.

Scott, a small typo correction if I may: "I wasn't use them" -> "I wasn't using them"

I also agree that syntax such as
dotnet new web
without any switch or uppercase, is better. That's how things tend to work on the Linux world. Less verbosity; consider CLI as a first class citizen and make it accomplish the most with minimum switches.

I've noticed in RC1 of .NET Core that
yo aspnet
and Visual Studio's File->New Project... did not yield the same result, so it's good to see that the team works towards unifying them. All platforms need to produce the very same code, or else one might ask "which one is the best?"

Cheers!
July 18, 2016 11:16
I prefer the more verbose -t and -l switches, it's just 2 characters and makes it clear what options are being set. Lowercase names for the options is a good change though.
July 18, 2016 11:59
Can't wait for the next phase of this cycle passes and we have pretty UI/GUI to compliment the wonky/arcane/error-prone CLI... once again. :P

So surprising to see the failings of DOS emerge from the very company that made Windows such a successful/great paradigm. Those who fail to learn from history... etc., etc. ...
July 18, 2016 12:00
I really like the way the yeoman generator guides you through a new project, letting you pick certain options and seeing what's available.

I think that would be a much better flow than the current "dotnet new" which defaults to a specific project type. I do agree with you, Scott, about your gripe with the -t argument, it doesn't seem necessary.

So my ideal would be that you can do "dotnet new" and get a list of project types you can select, maybe some options (Add Unit tests?) but each one of those options can be picked directly, i.e. "dotnet new aspnet/spa/angular2" or something along those lines. I think that would make the project types a lot more discoverable for new users who probably won't even think about appending additional arguments to "dotnet new".
July 18, 2016 13:05
Is there a way (by means of a switch or something) to know which types are available? Or are you supposed to just know?

For instance, can I get a list of available types when I do:
dotnet new --types


If other templates are coming (and maybe the possibility to create your own?), this might be useful to know which templates are available and installed where?

For languages, there are not so many, so I believe they can just remain in the help text 'dotnet new'.

July 18, 2016 13:15
It would be great if you could specify a Github repo as a template. It would allow the community to share templates and internally in companies share default frameworks.
July 18, 2016 13:34
Totally agree that the -t is superfluous, and I like the forward-slash idea for being more explicit.

One thing I'd really like to see would be
dotnet new
working with .sln and .xproj/.csproj files. So:

dotnet new solution


would create a
.sln
file in the current directory, and

dotnet new web -n MyWebApp


would create an empty web application with a .xproj file (for now) in a directory called
MyWebApp
and add it to the solution.

This would be a huge help to people working on different IDEs/editors/platforms, and especially people working with JetBrains Rider on Mac or Linux, which requires solution and project files and they're not going to change anything about that until the Tools RTM.
July 18, 2016 16:42
Hi Scott,

I would expect to see something like yeoman when I run "dotnet new" - that might compose a full commandline with all switches needed. I also expect this to be pluggable so others could provide new templates (as now in VS).

July 18, 2016 18:25
It's great to see a post focusing on doing things from the command line. So many posts assume everyone uses Visual Studio. I only run Linux at home, so I don't have access to Visual Studio. I've used the Yeoman aspnet generator in the past. It's good to know that I can create a new project with just the dotnet command. Is there any way to generate basic CRUD code from the command line?
July 18, 2016 18:59
I personally feel like "dotnet new web" implies you are creating a new project named web. This is the pattern I see with many CLI tools on Unix.
July 18, 2016 19:00
I enthusiastically agree that commands ala "dotnet new fsharp web my-app" would make for better UX.

I think it would also make sense to allow the user to set a default language in a config file so that if they are primarily a F# or VB user, they don't have to type out the language everytime they are scaffolding a new project (this would be in the same spirit of choosing your default language in VS)
July 18, 2016 19:10
I appreciate the hard work you guys put in, but honestly I really don't get the excitement around exposing new command line tools. The whole "point" of IDEs is so that you're not mussing around with the command line.

I think prioritization is pretty screwed up that this is where the focus is rather than on exposing this stuff through VS- the tool the majority of your customers use (and one they actually pay for). The tooling in VS always seems to be the last thing you guys update. If it wasn't for Mads Kristensen (a single man, who started Web Essentials on his own, without MS support) web tooling in VS would still suck. Unfortunately for many of us there's no Mads to fix the awful tooling everywhere else non-web in Visual Studio (like SSDT, T4 templates, Entity Framework).
Sam
July 18, 2016 19:43
Just like we do in VS File->New Project->Type-Of-Project->Name-of-Project and hit OK, so it would be helpful to follow similar flow as type "dotnet new Web [Optional-Name-Of-Project]" and hit OK. I think language selection would be less used, so that can be added as is into CLI command.
July 18, 2016 20:07
Thanks for the tip, I was wondering how you create a web project, because Yo-Generator doesn't always seem to be exactly in sync with the latest changes, or at least needs some minor modifications, due to the project's ever evolving nature.
July 18, 2016 21:35
I don't like the idea of dropping the -t. It seems too early to be taking the default 'argument' position and making it 'type'.

The dotnet cli should be consistent across all commands IMO.

dotnet <command> <command arguments with dashes>

You may think it's intuitive to have the type as the next parameter, but if I were to look at that without knowning better I would have assumed that's what you wanted to name the project. And once you make type the first parameter of new, you can't really change to anything else later on.

Seems better to just not have positional arguments, and just have them all specified with their dash prefix to me.


My opinions anyway.


July 18, 2016 21:37
Great writeup, thanks! Do you know whether we'll see Maven-style support for project inheritance?
July 18, 2016 21:52
I agree with Sam. What ever happened to the "Visual" in Visual Studio? I don't get all this love for command line utilities.
July 18, 2016 21:58
I agree with an option for empty/blank web application (like in visual studio).
Why not have "dotnet new -t web" create an empty web application, instead of including everything except the kitchen sink :p An option for the user to set this would be nice as well.
July 18, 2016 23:11
@Sam - The commandline tools are basically there like most of core... they're in complete copy mode of Node.js.

That they would copy node is fine, but copying the mistakes, dropping what is known to work from their own experience is where MS always goes wrong. This happened with Silverlight, as MS copied Adobe verbatim until later version where common sense took over.

Same will happen here, as first they toss .Net developers under the bus, then realize that a better way is to copy what's best of both platforms.

BTW, still waiting for core to work on Ubuntu, and would love to be able to add assemblies directly as references without nuget...
July 19, 2016 5:51
When I see a command line, I wanna kill.

When I see yet another command line, simple killing is not enough.

Typing commands isn't any cool in 2016. Like, what, am I going to develop an ASP.NET Core project via SSH while in mc running mcedit? No, I'm not.

I want .NET on Linux, but not Linux in .NET. God forbid.

Guys, guys, just take a survey why are people choosing .NET rather than some LAMP-junkyard.
July 19, 2016 15:20
@Andrew Github repo sounds like a good idea, for templates. With version metadata attached so we know what versions of the framework the template is targeting and so can be assured the generated template won't require modification to work with the latest libraries.
July 19, 2016 15:57
Yes. Lose the command line switches. Having to remember the switch always jars with my brain. Without the switches it's much more readable, and importantly memorable.

+1 for the command line :)
July 19, 2016 16:59
@Fallon, what's not working on Ubuntu (asp.net core web applications works for me)?

@Shrikant N: I think this will be added in the future, it was mentioned on the .net core cli repo that it will be added in the future.
July 20, 2016 12:18
Can someone explain what's the point of creating solutions, projects and classes on command line?
Who except a handful of vi enthusiasts will actually edit the created files in a command window without intellisense, sytnax highlight, inline error checking and other goodies?
You should rather spend the effort somewhere else.

@Scott
Re "dotnet new -t lib ... Not so useful, but good to know."
You may not create libraries but in real world developers have a lot because they don't want all files in a single project so they split, even add some to NuGet.
July 20, 2016 14:53
I think a good solution/approach here will be that `dotnet new` asked for the type of template you want to create (ala yoman) while keeping -t available.
July 20, 2016 17:35
I was playing with .NET Core on Mac the other day and found these commands quite handy. What I really miss, though, is the ability to scaffold new files, such as controllers or views, to speed up development.
July 20, 2016 21:17
I love the Visual Studio GUI. I also love commandline tools. We can never have too many tools in the tool shed. '-t' does seem a little heavy handed, but I can understand the concern of those who state that 'dotnet new web' implies the project name will be 'web.'

It seems like we could have a middle ground where if '-t' isn't specified, it could be prompted or intelligently look at the number of arguments. For example, 'dotnet new web MyWebProject' which could interpret the number of commands.

Whatever the end goal ends up being, thanks for keeping the discussion alive, Scott!
July 21, 2016 7:13
I prefer a workflow like below,
>dotnet new
-- add the nuget package that contains my preferred template to project.json (e.g. Angular 2)
>dotnet restore
>dotnet add-template angular2
July 23, 2016 20:54
I don't mind having or not having the switches, but I would like a way to list the types available. Maybe there is and I just don't know about it? I havn't seen anything that does that with the --help switch
July 24, 2016 14:53
Hi Scott,

I just created a new .NET core web application in VS 2015 update 3 and in the project.json file I see something like this

"tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},



"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},


Just wondering about the preview2-final stuff. Is the ASP.NET release full and final or still in development? preview2-final seems like incomplete stuff.

Thanks.
July 25, 2016 10:42
Could not agree more with @sam.
July 26, 2016 20:57
Really enjoyed the article, thanks Scott.
+1 for mentioning Steve Sanderson's generators, they are awesome for starting a new AspNet Core application with Angular2 or React. I found them very handy, after some struggle to setup a new Angular2 and AspNet Core project with Angular-CLI.
Absolutely recommended!
July 29, 2016 14:30
Hi Scott,

I am developing a MVC application in Asp.Net core framework. Now I want to deploy this website over raspberry pi or Dragon board 410 C (Window 10 IOT). Is there any other way to deploy it? I don't want to go with DNX. If yes then could you Guide me on that?

Thanks in advance.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.