【问题标题】:How to modify launchSettings.json in order to point to index.html如何修改 launchSettings.json 以指向 index.html
【发布时间】:2018-06-23 15:06:47
【问题描述】:

我基本上是 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

【问题讨论】:

    标签: asp.net iis asp.net-core visual-studio-2017 .net-core


    【解决方案1】:

    我下载了您的代码并将您的启动设置文件更改为完全限定的网址:

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

    运行结果:

    【讨论】:

    • 谢谢!不过,我刚刚尝试了您的建议,但它不起作用。
    • 当我添加 app.UseFileServer() 时它起作用了。你能更新你的答案吗?
    • 如果您找到了解决方案,您可以自己回答问题。抱歉我的没用。我做的唯一另一件事是将您的启动网址更改为完全合格的(我下载了您的代码)。将其添加到我的答案中,以防将来对其他人有帮助。很高兴您找到了解决方案。
    【解决方案2】:

    您需要将 .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,您应该一切顺利。

    【讨论】:

      猜你喜欢
      • 2016-11-15
      • 2018-03-26
      • 2011-06-23
      • 2020-06-12
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多