尝试在ASP.NET Core项目和其他位置的/api/values中提供Index.html [英] Trying to serve Index.html in ASP.NET Core project and /api/values elsewhere
问题描述
我有一个SPA应用程序,已经将其加载到/wwwroot目录中,并且它具有默认情况下要加载的index.html文件.然后,我希望正常的控制器文件能够工作.那是控制器应该运行的默认值.下面是我的创业公司.它确实呈现wwwroot/index.html,但/api/values无效(除非我注释掉UseStaticFiles,然后index.html无效).
I've got a SPA app that I've loaded into my /wwwroot directory and it has an index.html file that I want loaded by default. I then want the normal controller files to work. That is the default values controller should run. Below is my startup. It does render wwwroot/index.html but then /api/values does not work (unless I comment out UseStaticFiles, then index.html does not).
如何同时加载index.html和值控制器.
How can I get both index.html to load and also the values controller to work.
startup.cs
startup.cs
public void Configure(
IApplicationBuilder app)
{
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("index.html");
app.UseDefaultFiles(options);
app.UseStaticFiles();
app.UseMvc();
}
值控制器...
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
推荐答案
以下内容似乎可以解决我的问题.
The following seems to solve my problem.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("index.html");
app.UseDefaultFiles(options);
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
要进行测试,我添加了wwwroot/index.html
和一个简单的/home/contact
控制器和视图类(没有/home/index
控制器或视图).
To test it I added wwwroot/index.html
and a simple /home/contact
controller and view class (no /home/index
controller or view).
这篇关于尝试在ASP.NET Core项目和其他位置的/api/values中提供Index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!