时间:2023-07-25 00:33:01 | 来源:网站运营
时间:2023-07-25 00:33:01 来源:网站运营
手把手教你ASP.NET Core:http://ASP.NET Core 是一个新的开源和跨平台的框架,用于构建如 Web 应用、物联网(IoT)应用和移动后端应用等连接到互联网的基于云的现代应用程序。http://ASP.NET Core 应用可运行于 .NET Core 和完整的 .NET Framework 之上。 构建它的目的是为那些部署在云端或者内部运行(on-premises)的应用提供一个优化的开发框架。它由最小开销的模块化的组件构成,因此在构建你的解决方案的同时可以保持灵活性。你可以在 Windows、Mac 和 Linux 上跨平台的开发和运行你的 http://ASP.NET Core 应用。 http://ASP.NET Core 开源在 GitHub 上。
➜ codelover-blog git:(master) ✗ dotnet new web -n FirsrMVCThe template "ASP.NET Core Empty" was created successfully.This template contains technologies from parties other than Microsoft, see https://aka.ms/template-3pn for details.Processing post-creation actions...Running 'dotnet restore' on FirsrMVC/FirsrMVC.csproj... Restoring packages for /Users/liguobao/code/codelover-blog/FirsrMVC/FirsrMVC.csproj... Generating MSBuild file /Users/liguobao/code/codelover-blog/FirsrMVC/obj/FirsrMVC.csproj.nuget.g.props. Generating MSBuild file /Users/liguobao/code/codelover-blog/FirsrMVC/obj/FirsrMVC.csproj.nuget.g.targets. Restore completed in 1.97 sec for /Users/liguobao/code/codelover-blog/FirsrMVC/FirsrMVC.csproj.Restore succeeded.➜ codelover-blog git:(master) ✗ cd FirsrMVC➜ FirsrMVC git:(master) ✗ lsFirsrMVC.csproj Program.cs Startup.cs obj/ wwwroot/
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.Extensions.DependencyInjection;namespace FirsrMVC{ public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //直接往HTTP Response中写入"Hello World!",即在页面直接显示此字符 app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } }}
➜ FirsrMVC git:(master) ✗ dotnet runHosting environment: ProductionContent root path: /Users/liguobao/code/codelover-blog/FirsrMVCNow listening on: http://localhost:5000Application started. Press Ctrl+C to shut down.
MVC模式最早由Trygve Reenskaug在1978年提出[1],是施乐帕罗奥多研究中心(Xerox PARC)在20世纪80年代为程序语言Smalltalk发明的一种软件架构。MVC模式的目的是实现一种动态的程式设计,使后续对程序的修改和扩展简化,并且使程序某一部分的重复利用成为可能。除此之外,此模式通过对复杂度的简化,使程序结构更加直观。软件系统通过对自身基本部分分离的同时也赋予了各个基本部分应有的功能。
app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });
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 FirsrMVC{ 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.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }}
using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;namespace FirsrMVC.Controllers{ public class HomeController : Controller { public IActionResult Index() { return View(); } public IActionResult About() { return Json(new { name = "我的名字", success = true }); } }}
{ "name": "我的名字", "success": true}
<p>First View Page</p>>
namespace FirsrMVC{ public class Student { public string Name { get; set; } public int Age { get; set; } }}
public IActionResult Index() { Student student = new Student() { Name = "小明", Age = 16 }; return View(student); }
@model FirsrMVC.Student@if(Model !=null){ <p>@Model.Name<span>今年</span>@Model.Age<span>岁</span></p>}else{ <p>这里什么都没有.</p>}
关键词:把手