2017-08-28 516 views
0

我最近开始了一个Asp.Net核心应用程序。后来我想将它与Angular整合,但首先我想要一个能够用html代替cshtml的机制。Asp Net Core如何提供html静态页面?

如果我将cshtml的扩展名更改为html,我得到这个'InvalidOperationException:未找到视图'索引'。

我也试过在启动

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

,但它也没有工作。

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

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

回答

0

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

1.创建一个文件html内侧(wwwroot)中名称index.html

2.安装Microsoft.AspNet.StaticFiles经由的NuGet

3.添加UseStaticFiles在Startup.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") 
    }); 
} 

一样,如果你想的index.html是默认的文件http://<app>/StaticFiles/index.html

你的要求,这是一种功能,IIS一直有,那么

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

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

} 

希望它帮助你。您可以从this链接获得更多信息。

+0

你如何服务器index.cshtml? – Reft