繁体   English   中英

如何修改launchSettings.json以指向index.html

[英]How to modify launchSettings.json in order to point to index.html

我基本上是ASP.NET Core的新手。 我创建了一个Web API模板并设置了一个控制器,然后在wwwroot下手动创建了以下目录结构:

wwwroot/
  css/
    site.css
  js/
    site.js
  index.html

当我点击F5时,我想在浏览器中启动index.html 但是,我无法弄清楚如何做到这一点。 这是我的launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:63123/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "index.html",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
}

当我在Chrome中运行“IIS Express”命令时,我得到“没有找到网址的网页: http:// localhost:63123 / index.html ”。 为什么会这样?

我的应用程序的完整源代码可以在这里找到: https//github.com/jamesqo/Decaf/tree/webapp-2/Decaf.WebApp

我下载了您的代码并将您的launchsettings文件更改为完全限定的url:

"launchUrl": "http://localhost:63123/index.html",    

我还修改了你的StartUp.cs添加(app.UseStaticFiles();)它现在似乎工作:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseStaticFiles();

    app.UseMvc();
}

运行结果:

在此输入图像描述

您需要将.UseContentRoot添加到Program.cs文件的BuildWebHost方法中,如下所示:

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .Build();

然后,通过将.UseStaticFiles()添加到Startup类中的Configure方法,将程序配置为使用静态文件。 您还需要配置MVC中间件以侦听和处理传入请求。 您可以在app.UseMvc()中查看我在哪里配置路由。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

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

最后,导航到http:// localhost:63123 / index.html ,你应该很高兴。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM