简体   繁体   English

在Angular 2和ASP.NET Core中使用相对图像路径

[英]Using relative Image path in Angular 2 and ASP.NET Core

I was trying to get the path of a local image file, but I can't figure out where the relative paths are located. 我试图获取本地图像文件的路径,但我无法弄清楚相对路径的位置。 Normally I would get them out of wwwroot/images folder, but when I try to load them from there it fails. 通常我会把它们从wwwroot / images文件夹中取出来,但是当我尝试从那里加载它时它会失败。

Where do you get relative paths in Angular 2? 你在哪里获得Angular 2的相对路径? I used generator-aspnetcore-spa to create the application. 我使用generator-aspnetcore-spa来创建应用程序。


https://github.com/aspnet/JavaScriptServices https://github.com/aspnet/JavaScriptServices
http://blog.stevensanderson.com/2016/05/02/angular2-react-knockout-apps-on-aspnet-core/ http://blog.stevensanderson.com/2016/05/02/angular2-react-knockout-apps-on-aspnet-core/

For example in folder ClientApp/components/app/app.ts. 例如,在文件夹ClientApp / components / app / app.ts中。
Where would I put the image folder and how do I call its path ? 我将把图像文件夹放在哪里,如何调用它的路径?

In the Program.cs Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

the UseContentRoot defaults to the current directory from your app. UseContentRoot默认来自您应用的当前目录。

By calling app.UseStaticFiles(); 通过调用app.UseStaticFiles(); in the Startup class 在Startup类中

public class Startup
{
    ...
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        ...
        app.UseStaticFiles();

the static file middleware is enabled. 静态文件中间件已启用。 With default settings it will serve all files in the web root folder ( <content root>/wwwroot ). 使用默认设置,它将提供Web根文件夹( <content root>/wwwroot )中的所有文件。

If you look at the https://github.com/aspnet/JavaScriptServices Angular2 template, you will see in the webpack.config.js that the output is stored in 如果查看https://github.com/aspnet/JavaScriptServices Angular2模板,您将在webpack.config.js中看到输出存储在

output: {
    path: path.join(__dirname, 'wwwroot', 'dist'),
    filename: '[name].js',
    publicPath: '/dist/'
},

so that is <content root>/wwwroot/dist and the URL will be 所以这是<content root>/wwwroot/dist ,URL就是

 http://<whatever host and port>/dist/<my file>

The URL does not have to contain ClientApp (content root level, not accessable by static file middleware) but dist (wwwroot level). URL不必包含ClientApp (内容根级别,静态文件中间件无法访问),而是dist (wwwroot级别)。

Therefore put your images folder into ...\\wwwroot\\images and the image URL will be http://<whatever host and port>/images/<my image file> . 因此,将您的images文件夹放入...\\wwwroot\\images ,图像URL将是http://<whatever host and port>/images/<my image file>

IN .Net Core, the app needs to know to use static files, have you got 在.Net Core中,应用程序需要知道如何使用静态文件

app.UseStaticFiles();

in the Startup.cs? 在Startup.cs?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM