Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Asp Net Core serve html static pages?

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?

like image 787
GomuGomuNoRocket Avatar asked Aug 28 '17 09:08

GomuGomuNoRocket


1 Answers

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.

like image 145
Ashiquzzaman Avatar answered Sep 22 '22 06:09

Ashiquzzaman