Reputation: 21430
I have deployed an MVC Core 2.0 application and when I load any of my pages I can see that they javascript and css files are not pointing to the correct address.
For example, /lib/bootstrap-table...
should be /wwwroot/lib/bootstrap-table...
.
The application works perfectly when debugging locally via Visual Studio, however.
I have the following configuration in my Startup.cs:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Yet, the file paths are still not prefixed with /wwwroot
. Why doesn't MVC find the correct file path?
The Layout.cshtml file has the following; however, if I prefix the files with /wwwroot
then the files are no longer found when debugging:
<environment include="Development">
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/lib/fullcalendar/dist/fullcalendar.css" />
<link rel="stylesheet" href="~/lib/fullcalendar/dist/fullcalendar.print.css" media="print" />
<link rel="stylesheet" href="~/css/site.css" />
<link rel="stylesheet" href="~/lib/bootstrap-table/src/bootstrap-table.css" />
</environment>
<environment exclude="Development">
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
<link rel="stylesheet" href="~/lib/fullcalendar/dist/fullcalendar.min.css" />
<link rel="stylesheet" href="~/lib/fullcalendar/dist/fullcalendar.print.min.css" />
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
<link rel="stylesheet" href="~/lib/bootstrap-table/src/bootstrap-table.min.css" />
</environment>
I can see in my Program.cs file that ContentRoot is set to CurrentDirectory, but it works when debugging just not when deployed to a server.
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
}
Upvotes: 1
Views: 2969
Reputation: 21476
WebHost.CreateDefaultBuilder()
already sets up the default configurations, i.e., UseKestrel()
, UseContentRoot()
etc. You don't need to call them again.
Instead you only need this
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
And app.UseStaticFiles()
makes the files in wwwroot
folder (by default) servable. That is, a request like http://<your_domain>/<your_app>/css/site.css
will try to get the site.css
under the structure wwwroot/css/
.
You don't need to manually append wwwroot
in the static file urls.
Upvotes: 3