gpt4 book ai didi

asp.net - Asp Net Core如何服务html静态页面?

转载 作者:行者123 更新时间:2023-12-02 10:30:37 24 4
gpt4 key购买 nike

我最近启动了一个 Asp .Net Core 应用程序。后来我想将它与 Angular 集成,但首先我想要一种用 html '替换' cshtml 的机制。

如果我将扩展名从 cshtml 更改为 html 我会得到这个

'InvalidOperationException: The view 'Index' was not found'.

我也在启动中尝试过

app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

但它也不起作用。

可选的是将cshtml布局内容与html页面集成,但我认为这是不可能的。

所以,我的问题是,我可以简单地将所有cshtml替换为html吗?

最佳答案

静态文件通常位于 Web 根 (wwwroot) 文件夹中。默认情况下,这是我们可以直接从文件系统提供文件的唯一位置。

1.创建html (wwwroot) 内的文件名称 index.html

2.安装Microsoft.AspNet.StaticFiles通过 NuGet 打包

3.添加UseStaticFilesStartup.cs在配置方法下

public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder

// if you want to run outside wwwroot then use this
//request like http://<app>/StaticFiles/index.html
/* app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")),
RequestPath = new PathString("/StaticFiles")
});*/
}

如果你想在wwwroot之外运行静态文件,那么-

public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder

//request like http://<app>/StaticFiles/index.html
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")),
RequestPath = new PathString("/StaticFiles")
});
}

您的请求如http://<app>/StaticFiles/index.html

如果你想要index.html成为你的默认文件,这是IIS一直有的功能,那么

public void Configure(IApplicationBuilder app)  { 
app.UseIISPlatformHandler();
app.UseDeveloperExceptionPage();

app.UseRuntimeInfoPage();
app.UseDefaultFiles();
app.UseStaticFiles();

}

希望对您有帮助。您可以通过this了解更多信息链接。

关于asp.net - Asp Net Core如何服务html静态页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45915637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com