意図的なのかワザとなのか意地悪なのかしらないが、dotnet new には Visual Basic のプロジェクトを作成する機能はない。
機能はないのだが、下記を参照してカスタムテンプレートを作れば VB の ASP.NET MVC Core なプロジェクトテンプレートを作れる。
dotnet new のカスタム テンプレートを作成する | Microsoft Docs
https://docs.microsoft.com/ja-jp/dotnet/core/tutorials/create-custom-template
が、まあ、初手としてまずは「できる」ことを確認するために手作業でテンプレートを作ってみようって訳で。
.NET Core コンソールアプリから始める
プロジェクトで「コンソールアプリ(.NET Core)を作る
プロジェクトファイル(*.vbproj)を開いて、wwwroot と Microsoft.AspNetCore.All を追加する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | < Project Sdk = "Microsoft.NET.Sdk" > < PropertyGroup > < OutputType >Exe</ OutputType > < RootNamespace >web_vb</ RootNamespace > < TargetFramework >netcoreapp2.0</ TargetFramework > </ PropertyGroup > < ItemGroup > < Folder Include = "wwwroot\" /> </ ItemGroup > < ItemGroup > < PackageReference Include = "Microsoft.AspNetCore.All" Version = "2.0.8" /> </ ItemGroup > < ItemGroup > < DotNetCliToolReference Include = "Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version = "2.0.4" /> </ ItemGroup > </ Project > |
C# にある「Microsoft.VisualStudio.Web.CodeGeneration.Tools」も追加しているが、スキャフォールディングとかしない(多分できない)ので、必要ないかも。
Program.vb と Startup.vb を追加する
C# と合わせるために2つのファイルを追加する。
Program.vb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Imports System Imports System.Collections.Generic Imports System.IO Imports System.Linq Imports System.Threading.Tasks Imports Microsoft.AspNetCore Imports Microsoft.AspNetCore.Hosting Imports Microsoft.Extensions.Configuration Imports Microsoft.Extensions.Logging Module Program Sub Main(args As String ()) Console.WriteLine( "Hello ASP.NET MVC and VB World!" ) Dim host = WebHost.CreateDefaultBuilder(args). UseStartup(Of Startup)(). UseUrls( "http://*:5000" ).Build() host.Run() End Sub End Module |
Startup.vb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Threading.Tasks Imports Microsoft.AspNetCore.Builder Imports Microsoft.AspNetCore.Hosting Imports Microsoft.Extensions.Configuration Imports Microsoft.Extensions.DependencyInjection Imports Microsoft.Extensions.Logging Imports Microsoft.Extensions.Options Public Class Startup Public Sub New (configuration As IConfiguration) _Configuration = configuration End Sub Private _Configuration As IConfiguration Public ReadOnly Property Configuration As IConfiguration Get Return _Configuration End Get End Property Public Sub ConfigureServices(services As IServiceCollection) services.AddMvc() End Sub Public Sub Configure(app As IApplicationBuilder, env As IHostingEnvironment) If env.IsDevelopment() Then app.UseDeveloperExceptionPage() End If app.UseMvc() End Sub End Class |
コントローラクラスを書き替える
ValuesController.vb を作る
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Threading.Tasks Imports Microsoft.AspNetCore.Mvc <Route( "api/[controller]" )> Public Class ValuesController Inherits Controller <HttpGet> Public Function [ Get ]() As IEnumerable(Of String ) Return New String () { "value1" , "value2" } End Function <HttpGet( "{id}" )> Public Function [ Get ](id As Integer ) As String Return "value" End Function <HttpPost> Public Sub Post(<FromBody> value As String ) End Sub <HttpPut( "{id}" )> Public Sub Put(id As Integer , <FromBody> value As String ) End Sub <HttpDelete( "{id}" )> Public Sub Delete(id As Integer ) End Sub End Class |
HttpGet のところは、Getメソッドが標準とダブるので [Get] のようにしてあるが、GetList とか名前を付けてもよいと思う。
実行する
http://localhost:5000/api/values を実行すると、web api が動いていることが分かる。
Razor は使えるのか?
ASP.NET Core な Web API を VB で作ることができることは解ったのだが、じゃあ MVC の Razor は使えるのかどうか?ってのは未だ試していない。
dotnet new page コマンドを使うと C# の Razor(*.cshtml)を出力してくれるので、このあたりを追加すると VB でも使えるようになるのは?とか。