简体   繁体   中英

How to modify launchSettings.json in order to point to index.html

I'm largely a newbie to ASP.NET Core. I created a Web API template and setup a controller, then manually created the following directory structure under wwwroot :

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

When I hit F5, I want to launch index.html in the browser. However, I can't figure out how to do this. Here's my 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 ". Why is this happening?

The full source of my app is available here: https://github.com/jamesqo/Decaf/tree/webapp-2/Decaf.WebApp .

I downloaded your code and changed your launchsettings file to the fully qualified url:

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

I also amended your StartUp.cs adding (app.UseStaticFiles();) and it now seems to work:

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:

    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. You also need to configure the MVC middleware to listen to and handle the incoming request. You can see where I have configured routes in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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