Asp.net core Razor 頁面

建立asp.net core 空項目->MyWebhtml

修改Startup.cs啓動文件添加Razor頁面支持:app

 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.cshtmlasp.net

Index.cshtmlui

 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.cshtmlthis

 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.csspa

 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.net

添加<a href="Index2">Index2</a>完成頁面跳轉code

或輸入地址http://localhost:端口號/Index2進行網頁跳轉server

相關文章
相關標籤/搜索