服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - asp.net core项目中如何使用html文件

asp.net core项目中如何使用html文件

2020-05-23 15:45河图书卦 ASP.NET教程

这篇文章主要给大家介绍了关于asp.net core项目中如何使用html文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面是随着小编来一起学习学习吧。

前言

大家应该都知道,在html">asp.net core 项目中,使用html文件一般通过使用中间件来提供服务:

打开 NuGet程序管理控制台

输入install-package Microsoft.aspnetcore.staticfiles 进行添加

ASP.NET Core static files middleware. Includes middleware for serving static files, directory browsing, and default files.

在Startup.cs中使用服务:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 MyWeb
{
  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)
    {
      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)
    {
      app.UseStaticFiles();
      app.UseMvc();
    }
  }
}

在wwwroot下添加Baidu.html

?
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Baidu</title>
</head>
<body>
  <a href="http://www.baidu.com" target="_self"><em>进入百度</em></a>
</body>
</html>

修改Index.cshtml,添加访问链接

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

运行MyWeb在Index首页进行访问

或者输入地址http://localhost:端口号/Baidu.html

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.cnblogs.com/szz-55555/p/8461489.html

延伸 · 阅读

精彩推荐