.NetCore WebAPI 使用 wwwroot 下的 index.html 作为首页
第一步
文件:Program.cs
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
第二步
创建文件:wwwroot/index.html
Hello,World!
第三步
创建文件:Controllers/HomeController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace zgcwkj.Controllers
{
/// <summary>
/// Home
/// </summary>
[AllowAnonymous]
public class HomeController : Controller
{
/// <summary>
/// WebHost
/// </summary>
private IWebHostEnvironment _IWebHost { get; }
/// <summary>
/// Home
/// </summary>
public HomeController(IWebHostEnvironment iWebHost)
{
this._IWebHost = iWebHost;
}
/// <summary>
/// Index
/// </summary>
public IActionResult Index()
{
var rootPath = _IWebHost.ContentRootPath;
//停用浏览器缓存
Response.Headers.Add("Cache-Control", "no-cache");
Response.Headers.Add("Pragma", "no-cache");
Response.Headers.Add("Expires", "-1");
//请求的域名
var host = Request.Host.Host;
//
var rootIndex = $"{rootPath}/wwwroot/index.html";
if (!System.IO.File.Exists(rootIndex))
{
return View("No File");
}
var fileResult = PhysicalFile(rootIndex, "text/html; charset=UTF-8");
return fileResult;
}
}
}
第四步
运行项目查看结果
关联
https://stackoverflow.com/questions/40646815
版权属于:zgcwkj
本文链接:https://zgcwkj.com/archives/206.html
转载声明:请注明本文章的标题及内容的出处和声明,谢谢
评论已关闭