问题
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).
How can I get both index.html to load and also the values controller to work.
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();
}
the values controller...
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
回答1:
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?}");
});
}
}
To test it I added wwwroot/index.html
and a simple /home/contact
controller and view class (no /home/index
controller or view).
来源:https://stackoverflow.com/questions/48144536/trying-to-serve-index-html-in-asp-net-core-project-and-api-values-elsewhere