Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Image path on ASP.NET Core + Angular 2 template for Visual Studio

I need to add a static image as shown below.Can you tell me why I cannot show the image on home page as shown below ? i.e. It's not working.

Here I'm using this ASP.NET Core Template Pack

Here is nice article about it from Steven Sanderson

enter image description here

\home\home.component.html

<img src="{{heroImageUrl}}" style="height:30px">

home.component.ts

import { Component } from '@angular/core';

@Component({
    selector: 'home',
    template: require('./home.component.html')
})
export class HomeComponent {

    public heroImageUrl ="./image/employee_management.jpg";
}

Error :

it says like this Failed to load resource: the server responded with a status of 404 (Not Found).May be a path issue.how can I give it correctly ? Image is there as shown above.

like image 372
Sampath Avatar asked Jan 02 '17 12:01

Sampath


People also ask

How do I add an angular project to an existing .NET Core Web API project?

To use publish, create your JavaScript project using Visual Studio 2022 version 17.3 or later. In Solution Explorer, right-click the ASP.NET Core project and choose Add > Project Reference. Select the Angular project and choose OK. Right-click the ASP.NET Core project in Solution Explorer and choose Unload Project.

How do I run a .NET Core in angular app?

Run dotnet run to start the app. The project template creates an ASP.NET Core app and an Angular app. The ASP.NET Core app is intended to be used for data access, authorization, and other server-side concerns. The Angular app, residing in the ClientApp subdirectory, is intended to be used for all UI concerns.

Can we use angular in asp net?

Select the template, add your project name, and click OK to create your Angular 7 application using ASP.NET Core. You can see that a new Angular 7 project has been created. Also, we can see the ASP.NET Core Controller and Angular 7 Client App folder from the Solution Explorer.


2 Answers

Since you're using Webpack to bundle these files, you just need to use require. Change your TypeScript code to this:

public heroImageUrl = require("./image/employee_management.jpg");

... and you're done. Your existing Webpack configuration is already set up to bundle .jpg files using file-loader, so the require call will return the URL of the bundled image.


Note: The OP didn't mention, but they are using the ASP.NET Core + Angular 2 template here, which has Webpack all set up already. Therefore this ends up being a a Webpack question, not an Angular question, so the question title is misleading.

like image 172
Steven Sanderson Avatar answered Sep 22 '22 01:09

Steven Sanderson


Include image folder in angular-cli.json under assets node like below

 "assets": [
        "assets",
        "favicon.ico",
        "fonts",
        "images"
      ],
like image 36
Aniket Avatar answered Sep 23 '22 01:09

Aniket