How can Asp Net Core serve html static pages?

南笙酒味 提交于 2020-01-01 09:55:44

问题


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.

If I change extension from cshtml to html I get this

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

Also I tried in Startup

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

but it also didn't work.

Optional is to integrating cshtml layout content with html pages, but I think this is impossible.

So, my question is, can I simply replace all cshtml with html?


回答1:


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.create a html file inside (wwwroot) name index.html

2.install Microsoft.AspNet.StaticFiles package via NuGet

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")
    });*/
}

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")
    });
}

your request like http://<app>/StaticFiles/index.html

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.



来源:https://stackoverflow.com/questions/45915637/how-can-asp-net-core-serve-html-static-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!