2

I have a project in ASP.Net Core that need to include a image from a resource file (to generate a PDF).

So, I create a new resource file using Visual Studio (Add > New Item > Resources File), named Resource.resx

Using the Managed Resource Editor (default editor of Visual Studio), I included a new image named logo.png. A new file named Resource.Designer.cs was created with a method listed below:

public static System.Drawing.Bitmap logo {
    get {
        object obj = ResourceManager.GetObject("logo", resourceCulture);
        return ((System.Drawing.Bitmap)(obj));
    }
}

Now, only to test, I created the following code:

var logo = Resources.logo;

This threw a new exception, with the following content:

An unhandled exception of type 'System.InvalidCastException' occurred. Additional Information: Unable to cast object of type 'System.String' to type 'System.Drawing.Bitmap'.

I tried all from this link too: https://weblogs.asp.net/jeff/beating-localization-into-submission-on-asp-net-5

but the results are the same.

If I make this code on a console application, everything works correctly.

3 Answers 3

5

I found another approach that worked good for my problem.

http://codeopinion.com/asp-net-core-embedded-resource/

Just need to create a folder on project (Resources in my case), and then, in project.json, I included the following code:

"buildOptions": {
  "embed": ["Resources/**/*"]
}

and then, my code:

var assembly = Assembly.GetExecutingAssembly();
var logoStream = assembly.GetManifestResourceStream("ProjectNamespace.Resources.logo.png");
Sign up to request clarification or add additional context in comments.

Comments

5

If you are using .net core 3.1 api, you may-

  1. Add services.AddLocalization(); in ConfigureServices method (Startup.cs)

  2. Add Resource file in the project say Resource.en-US.resx, add TestKey in Name and Hello in Value column for testing purpose.

  3. Add a class file in the same hierarchy with name as Resource.cs

  4. In controller, add a variable-

    private readonly IStringLocalizer _localizer;

and inject it in constructor-

public TestController(IStringLocalizer<Resource> localizer)
        {            
            _localizer = localizer;
        }
  1. Read the value of resource names as- _localizer["TestKey"] and you will get Hello (i.e. entered in step#2) More details at- [https://www.syncfusion.com/blogs/post/how-to-use-localization-in-an-asp-net-core-web-api.aspx]

Comments

0

I'm using Visual Studio 2017 (csproj files) and this solution worked for me: https://stackoverflow.com/a/39368856/812610

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties. In Properties window and change Build Action to Embedded Resource.

The embedded resource's name is "[DefaultNamespace].[Folder].filename". I saved a cert (cert.pfx) to "Resources" folder so for me it's "MyProjectName.Resources.cert.pfx"

And this was added to my .csproj:

<ItemGroup>
    <None Remove="Resources\testcert.pfx" />
  </ItemGroup>
<ItemGroup>
    <EmbeddedResource Include="Resources\testcert.pfx" />
  </ItemGroup>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.