Asp.net core Razor 页面

创建asp.net core 空项目->MyWeb

修改Startup.cs启动文件添加Razor页面支持:

 1  public void ConfigureServices(IServiceCollection services)
 2         {
 3             services.AddMvc();
 4         }
 5 
 6         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
 7         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 8         {
 9             app.UseMvc();
10         }

添加文件夹Pages并添加Razor页面 Index.cshtml和Index2.cshtml

Index.cshtml

 1 @page
 2 @model MyWeb.Pages.IndexModel
 3 @{
 4     ViewData["Title"] = "Index";
 5 }
 6 
 7 <h2>Index</h2>
 8 <a href="Index2">Index2</a>
 9 <h1>Hello, world!</h1>
10 <h2>The time on the server is @DateTime.Now</h2>

Index2.cshtml

 1 @page
 2 @model MyWeb.Pages.Index2Model
 3 @{
 4     ViewData["Title"] = "Index2";
 5 }
 6 
 7 <h2>Index2</h2>
 8 <h2>Separate page model</h2>
 9 <p>
10     @Model.Message
11 </p>

修改index2.cshtml.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Mvc;
 6 using Microsoft.AspNetCore.Mvc.RazorPages;
 7 
 8 namespace MyWeb.Pages
 9 {
10     public class Index2Model : PageModel
11     {
12         public string Message { get; private set; } = "PageModel in C#";
13         public void OnGet()
14         {
15             Message += $" Server time is { DateTime.Now }";
16         }
17     }
18 }

运行MyWeb,会自动匹配打开主页Index

添加<a href="Index2">Index2</a>完成页面跳转

或输入地址http://localhost:端口号/Index2进行网页跳转

 

posted @ 2018-02-23 10:28  小笨0104  阅读(402)  评论(0编辑  收藏  举报