Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlEncoder error in Asp.Net MVC 5

Tags:

c#

asp.net-mvc

I am getting this error when I compile: Error-CS0103 "The name 'HtmlEncode' does not exist in the current context"

I am using Visual Studio 2015 Community edition and MVC.

The code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;

namespace MvcMovie.Controllers
{
    public class HelloWorldController : Controller
    {
        // 
        // GET: /HelloWorld/ 

        public string Index()
        {
            return "This is my default action...";
        }

        // 
        // GET: /HelloWorld/Welcome/ 

        public string Welcome(string name, int numTimes = 1)
        {
            return HtmlEncoder.Default.HtmlEncode(
                "Hello " + name + ", NumTimes is: " + numTimes);
        }
    }
}

I cannot find HtmlEncoder to add to References.
Can you see what I am doing wrong?

Thanks!

like image 302
John Miller Avatar asked Dec 24 '22 11:12

John Miller


2 Answers

Try this:

    HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
like image 153
j.v. Avatar answered Jan 18 '23 03:01

j.v.


you are missing :

using Microsoft.Extensions.WebEncoders;
like image 38
Elazaron Avatar answered Jan 18 '23 03:01

Elazaron