下面是一个在ASP.NET Core 2.1中在特定的HTML页面上显示字符串列表的示例代码:
using Microsoft.AspNetCore.Mvc;
public class HomeController : Controller
{
public IActionResult Index()
{
// 创建一个字符串列表
var strings = new List { "string1", "string2", "string3" };
// 将字符串列表传递给视图
return View(strings);
}
}
@model List
字符串列表:
@foreach (var str in Model)
{
- @str
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ...
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
// ...
}
这样,当访问http://localhost:port/Home/Index
时,将显示一个包含字符串列表的HTML页面。