[英]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.