Click here to Skip to main content
15,879,326 members
Articles / Web Development / ASP.NET / ASP.NET Core
Tip/Trick

Creating RazorPage Apps Using the CLI

Rate me:
Please Sign up or sign in to vote.
3.30/5 (4 votes)
4 Jun 2018CPOL1 min read 9.3K   2   4
Creating RazorPage apps using the CLI

While I was creating a recent test application with Razor Pages, I found myself clicking the "new file" button in VS Code too many times when I wanted to add a new Page.

vscode new file

I really love the .NET CLI and with every release, there seems to be something I discover that has been added to either the core functionality or the templates. Running dotnet new this time around, I fell upon the page template...

what is this?

Within your application, run dotnet new page and a new Razor Page is added to the project. See the -h|--help some items removed for brevity.

$ dotnet new page -h

  -n, --name          The name for the output being created. If no name is specified, 
                      the name of the current directory is used.
  -o, --output        Location to place the generated output.

Razor Page (C#)  
Author: Microsoft  
Description: A Razor page with or without a page model  
Options:  
  -na|--namespace     namespace for the generated code
                      string - Optional
                      Default: MyApp.Namespace

  -np|--no-pagemodel  create page without a PageModel
                      bool - Optional
                      Default: false / (*) true


* Indicates the value used if the switch is provided without a value.

Given this information, I was able to quickly add a new page.

Should this be a default setting in the CLI? See Issue# 9194.

$ dotnet new page -n Index -na myapp.Pages.Area -o Pages/Area

Creating a variable for the namespace made this shorter.

$ ns=-na=myapp.Pages

Then I can just create a new page using the shorter command.

$ dotnet new page -n Index $ns.Area -o Pages/Area

Razor Pages is a super easy way to get going on web app and now adding the additional pages too using the CLI is just as easy. Looking forward to some of the other scaffolding pieces making it into the CLI as well as the previous yo aspnet functionality I helped build back in the beta days.

Image 3

This article was originally posted at http://feedproxy.google.com/~r/Tattoocoder/~3/VRBdAb0hqvQ

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I work on Azure, ASP.NET Core content and Open Source, speak at national and community events while helping teams architect web and cloud applications.

Comments and Discussions

 
SuggestionCool, then what about... Pin
Alaa Ben Fatma4-Jun-18 7:02
professionalAlaa Ben Fatma4-Jun-18 7:02 
Now that we know that we can create Razor pages using CLI, what about adding a shortcut that does that for you? CTRL + R + N.
Luck!
GeneralRe: Cool, then what about... Pin
Shayne P Boyer5-Jun-18 7:09
Shayne P Boyer5-Jun-18 7:09 
GeneralRe: Cool, then what about... Pin
Alaa Ben Fatma5-Jun-18 7:59
professionalAlaa Ben Fatma5-Jun-18 7:59 
GeneralRe: Cool, then what about... Pin
Shayne P Boyer6-Jun-18 2:19
Shayne P Boyer6-Jun-18 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.