温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

asp.net core项目中使用html文件的方法

发布时间:2021-02-18 10:57:21 来源:亿速云 阅读:403 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关asp.net core项目中使用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中使用服务:

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

<!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,添加访问链接

@page
@model MyWeb.Pages.IndexModel
@{
  ViewData["Title"] = "Index";
}

<h3>Index</h3>
<a href="Index2">Index2</a>
<a href="Baidu.html" target="_self">Baidu</a>
<hr />
<a href="Customers">CustomersIndex</a>
<h2>Hello, world!</h2>
<h3>The time on the server is @DateTime.Now</h3>

运行MyWeb在Index首页进行访问

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

关于“asp.net core项目中使用html文件的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI