Asp Net Core如何提供html静态页面? [英] How can Asp Net Core serve html static pages?
问题描述
我最近启动了一个Asp .Net Core
应用程序.后来我想将其与Angular集成,但是首先我想要一种将cshtml
替换为html
的机制.
I have recently started an Asp .Net Core
app. Later I wanted to integrate it with Angular, but first I wanted a mechanism that will 'replace' cshtml
with html
.
如果将扩展名从cshtml
更改为html
,我会得到
If I change extension from cshtml
to html
I get this
"InvalidOperationException:找不到视图索引"".
'InvalidOperationException: The view 'Index' was not found'.
我也在Startup中尝试过
Also I tried in Startup
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
但是它也不起作用.
可选的方法是将cshtml布局内容与html页面集成在一起,但是我认为这是不可能的.
Optional is to integrating cshtml layout content with html pages, but I think this is impossible.
所以,我的问题是,我可以简单地将所有cshtml替换为html吗?
So, my question is, can I simply replace all cshtml with html?
推荐答案
静态文件通常位于Web根(wwwroot)文件夹中.默认情况下,这是我们可以直接从文件系统直接提供文件的唯一位置
Static files are typically located in the web root (wwwroot) folder.By default, that is the only place where we can serve up files directly from the file system.
1.在(wwwroot)名称index.html
1.create a html
file inside (wwwroot) name index.html
2.通过NuGet安装Microsoft.AspNet.StaticFiles
软件包
2.install Microsoft.AspNet.StaticFiles
package via NuGet
3.在Startup.cs
的配置方法下添加UseStaticFiles
3.Add UseStaticFiles
in Startup.cs
under Configure methode
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
之外运行静态文件,则-
if you want to run Static Files outside wwwroot
, then-
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一直具有的功能,然后
if you want index.html
to be your default file, this is a feature that IIS has always had,then
public void Configure(IApplicationBuilder app) {
app.UseIISPlatformHandler();
app.UseDeveloperExceptionPage();
app.UseRuntimeInfoPage();
app.UseDefaultFiles();
app.UseStaticFiles();
}
希望对您有所帮助.您可以从此链接中获得更多信息.
hopefully it's help you. You can more information from this link.
这篇关于Asp Net Core如何提供html静态页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!