简体   繁体   中英

MVC Core Root Path Is Not Prefixed with wwwroot

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();
}

WebHost.CreateDefaultBuilder() already sets up the default configurations, ie, 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.

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