简体   繁体   English

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

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

I'm largely a newbie to ASP.NET Core. 我基本上是ASP.NET Core的新手。 I created a Web API template and setup a controller, then manually created the following directory structure under wwwroot : 我创建了一个Web API模板并设置了一个控制器,然后在wwwroot下手动创建了以下目录结构:

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

When I hit F5, I want to launch index.html in the browser. 当我点击F5时,我想在浏览器中启动index.html However, I can't figure out how to do this. 但是,我无法弄清楚如何做到这一点。 Here's my launchSettings.json : 这是我的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"
      }
    }
}

When I run the "IIS Express" command in Chrome, I get "No webpage was found for the web address: http://localhost:63123/index.html ". 当我在Chrome中运行“IIS Express”命令时,我得到“没有找到网址的网页: http:// localhost:63123 / index.html ”。 Why is this happening? 为什么会这样?

The full source of my app is available here: https://github.com/jamesqo/Decaf/tree/webapp-2/Decaf.WebApp . 我的应用程序的完整源代码可以在这里找到: https//github.com/jamesqo/Decaf/tree/webapp-2/Decaf.WebApp

I downloaded your code and changed your launchsettings file to the fully qualified url: 我下载了您的代码并将您的launchsettings文件更改为完全限定的url:

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

I also amended your StartUp.cs adding (app.UseStaticFiles();) and it now seems to work: 我还修改了你的StartUp.cs添加(app.UseStaticFiles();)它现在似乎工作:

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

    app.UseStaticFiles();

    app.UseMvc();
}

Result on running: 运行结果:

在此输入图像描述

You need to add .UseContentRoot to the BuildWebHost method of your Program.cs file, like this: 您需要将.UseContentRoot添加到Program.cs文件的BuildWebHost方法中,如下所示:

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

then, configure the program to use static files by adding .UseStaticFiles() to the Configure method in your Startup class. 然后,通过将.UseStaticFiles()添加到Startup类中的Configure方法,将程序配置为使用静态文件。 You also need to configure the MVC middleware to listen to and handle the incoming request. 您还需要配置MVC中间件以侦听和处理传入请求。 You can see where I have configured routes in app.UseMvc(). 您可以在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?}");
            });
        }

Lastly, navigate to http://localhost:63123/index.html and you should be good to go. 最后,导航到http:// localhost:63123 / index.html ,你应该很高兴。

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

相关问题 launchSettings.json 命令名称用法 - launchSettings.json commandName usage Asp.Net Core 在 launchSettings.json 中更改 url 不起作用 - Asp.Net Core change url in launchSettings.json not working launchSettings.json launchUrl 不起作用“api/values” - launchSettings.json launchUrl doesn't work "api/values" 更改launchSettings.json中的端口会显示“无法访问此站点” - Changing port in launchSettings.json gives “This site can’t be reached” 为什么存在 aspNetCore web.config 部分时,launchSettings.json 配置文件中的 environmentVariables 不加载? - Why do environmentVariables from launchSettings.json profiles not load when aspNetCore web.config sections exist? 您可以在 ASP.NET Core 项目中更改 launchSettings.json 的位置吗? - Can you change the location of launchSettings.json in an ASP.NET Core project? 尽管在本地环境中具有相同的 launchSettings.json,但使用了不同的 URL - Different URLs used despite having same launchSettings.json in local enironment 如何在Visual Studio 2015中运行index.html文件 - How to run index.html file in Visual Studio 2015 如何调用 Angular-CLI .NET Core 中的 index.html? - How index.html in Angular-CLI .NET Core is called? 如何通过IIS从URL上删除Index.html - How to cosmetically remove Index.html from URL with IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM