時間:2023-07-25 00:33:01 | 來源:網(wǎng)站運營
時間:2023-07-25 00:33:01 來源:網(wǎng)站運營
手把手教你ASP.NET Core:http://ASP.NET Core 是一個新的開源和跨平臺的框架,用于構(gòu)建如 Web 應(yīng)用、物聯(lián)網(wǎng)(IoT)應(yīng)用和移動后端應(yīng)用等連接到互聯(lián)網(wǎng)的基于云的現(xiàn)代應(yīng)用程序。http://ASP.NET Core 應(yīng)用可運行于 .NET Core 和完整的 .NET Framework 之上。 構(gòu)建它的目的是為那些部署在云端或者內(nèi)部運行(on-premises)的應(yīng)用提供一個優(yōu)化的開發(fā)框架。它由最小開銷的模塊化的組件構(gòu)成,因此在構(gòu)建你的解決方案的同時可以保持靈活性。你可以在 Windows、Mac 和 Linux 上跨平臺的開發(fā)和運行你的 http://ASP.NET Core 應(yīng)用。 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世紀(jì)80年代為程序語言Smalltalk發(fā)明的一種軟件架構(gòu)。MVC模式的目的是實現(xiàn)一種動態(tài)的程式設(shè)計,使后續(xù)對程序的修改和擴展簡化,并且使程序某一部分的重復(fù)利用成為可能。除此之外,此模式通過對復(fù)雜度的簡化,使程序結(jié)構(gòu)更加直觀。軟件系統(tǒng)通過對自身基本部分分離的同時也賦予了各個基本部分應(yīng)有的功能。
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>}
關(guān)鍵詞:把手
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。