RonC
RonC

Reputation: 33791

How to access Encoder.Default.HtmlEncode method from code library in Asp.Net 5 (core) mvc solution?

I have an Asp.Net 5 (core) solution using Asp.Net 5 RC1 in Visual Studio 2015 Update 2 which has two projects, an Asp.net 5 website project and a "Class Library (Package)" project.

I would like to be able to call the following method from within my class library:

 HtmlEncoder.Default.HtmlEncode(text) 

However, I can't figure out what reference needs to be set on the project to make the method available. I have noticed that I can access the method from the Asp.net 5 (core) website project if I add the following using statement to the file containing the method call:

using Microsoft.Extensions.WebEncoders;

But when I try to add this using statement to a file in the class library project Visual Studio indicates that no such namespace exists due to a missing project reference.

I've searched and searched and I can't figure out what project reference is needed to access that namespace. I assume it's a nuget package reference that's needed but when I go to "Manage NuGet Packages" for the project, the list of available packages is empty.

What project reference do I need and how can I get it?

Intellisense is complaining about the method and the using statement.  The website project is called Learning and the class lib is called HelpersI don't have any options for adding nuget packages for the project!?

Upvotes: 0

Views: 2774

Answers (2)

Vincent Cohen
Vincent Cohen

Reputation: 61

I have just installed the ASP.NET Core version and I have the same problem with a sample provided by Microsoft in a doc called ASP.NET MVC 6 Documentation, Release.

The solution I found is :

Replace using Microsoft.Extensions.WebEncoders; by using Microsoft.AspNetCore.Mvc; using System.Text.Encodings.Web;

And replace return HtmlEncoder.Default.HtmlEncode( by return HtmlEncoder.Default.Encode(

and it works !

Upvotes: 3

agua from mars
agua from mars

Reputation: 17424

Add a reference to "Microsoft.Extensions.WebEncoders ": "1.0.0-rc1-final" in your project.json

Upvotes: 3

Related Questions