using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging;
namespace netcore_mvc { public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); }
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options;
namespace netcore_mvc { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc;
namespace netcore_mvc.Controllers { // ValuesController is the equivalent // `ValuesController` of the Iris 8.3 mvc application. [Route("api/[controller]")] public class ValuesController : Controller { // Get handles "GET" requests to "api/values/{id}". [HttpGet("{id}")] public string Get(int id) { return "value"; }
// Put handles "PUT" requests to "api/values/{id}". [HttpPut("{id}")] public void Put(int id, [FromBody]string value) { }
// Delete handles "DELETE" requests to "api/values/{id}". [HttpDelete("{id}")] public void Delete(int id) { } } }
运行 .NET Core web 服务项目:
1 2 3 4 5 6 7
$ cd netcore-mvc $ dotnet run -c Release Hosting environment: Production Content root path: C:\mygopath\src\github.com\kataras\iris\_benchmarks\netcore-mvc Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using netcore_mvc_templates.Models;
namespace netcore_mvc_templates.Controllers { public class HomeController : Controller { public IActionResult Index() { return View(); }
public IActionResult About() { ViewData["Message"] = "Your application description page.";
return View(); }
public IActionResult Contact() { ViewData["Message"] = "Your contact page.";
return View(); }
public IActionResult Error() { return View(new ErrorViewModel { Title = "Error", Code = 500}); } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging;
namespace netcore_mvc_templates { public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); }
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection;
namespace netcore_mvc_templates { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { /* An unhandled exception was thrown by the application. System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered. Solution: Use AddMvc() instead of AddMvcCore() in Startup.cs and it will work. */ // services.AddMvcCore(); services.AddMvc(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseStaticFiles();
These files are quite long to be shown in this article but you can view them at: https://github.com/kataras/iris/tree/master/_benchmarks/netcore-mvc-templates
运行 .NET Core 服务项目:
1 2 3 4 5 6 7
$ cd netcore-mvc-templates $ dotnet run -c Release Hosting environment: Production Content root path: C:\mygopath\src\github.com\kataras\iris\_benchmarks\netcore-mvc-templates Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.
const ( // templatesDir is the exactly the same path that .NET Core is using for its templates, // in order to reduce the size in the repository. // Change the "C\\mygopath" to your own GOPATH. templatesDir = "C:\\mygopath\\src\\github.com\\kataras\\iris\\_benchmarks\\netcore-mvc-templates\\wwwroot" )
/* ../netcore-mvc-templates/wwwroot/css ../netcore-mvc-templates/wwwroot/images ../netcore-mvc-templates/wwwroot/js ../netcore-mvc-templates/wwwroot/lib ../netcore-mvc-templates/wwwroot/favicon.ico views/shared/layout.html views/shared/error.html views/about.html views/contact.html views/index.html These files are quite long to be shown in this article but you can view them at: https://github.com/kataras/iris/tree/master/_benchmarks/iris-mvc-templates */
运行 Go 服务项目:
1 2 3 4 5 6
$ cd iris-mvc-templates $ go run main.go Now listening on: http://localhost:5000 Application started. Press CTRL+C to shut down.