‘Extensions’ and ‘Script’ does not exist in namespace ‘System.Web’

Hi im using razor pages asp.net c# and Im trying to use System.Web.Extensions and using System.Web.Script; and
using System.Web.Script.Serialization;
Because if i remove these references i get other errors:

‘error CS0103: The name ‘CommandType’ does not exist in the current
context’

‘error CS0246: The type or namespace name ‘JavaScriptSerializer’ could
not be found’

I tried looking on internet for solutions but ive had no success. Here is my code, hopefully you guys can help me

using Microsoft.AspNetCore.SignalR;
using System;
using System.Web;
using System.Web.Extensions;
using System.Web.Script;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using myWebApp.Database;
using myWebApp.Models;
using myWebApp.Pages;
using myWebApp.Hubs;
using Npgsql;

namespace myWebApp.Controllers
{
    public class SpeedListener :Controller
    {
        private IHubContext<speedalarmhub> _hubContext;
        private IMemoryCache _cache;
        public SpeedListener(IHubContext<speedalarmhub> hubContext,IMemoryCache cache)
        {
            _hubContext = hubContext;
            _cache = cache; 
        }
        public string cs = Database.Database.Connector();
        public void ListenForAlarmNotifications()
        {
            NpgsqlConnection conn = new NpgsqlConnection(cs);
            conn.StateChange += conn_StateChange;
            conn.Open();
            var listenCommand = conn.CreateCommand();
            listenCommand.CommandText = $"listen notifyalarmspeed;";
            listenCommand.ExecuteNonQuery();
            conn.Notification += PostgresNotificationReceived;
            _hubContext.Clients.All.SendAsync(this.GetAlarmList());
            while (true)
            {
                conn.Wait();
            }
        }
        private void PostgresNotificationReceived(object sender, NpgsqlNotificationEventArgs e)
        {

            string actionName = e.Payload.ToString();
            string actionType = "";
            if (actionName.Contains("INSERT"))
            {
                actionType = "Insert";
            }
            _hubContext.Clients.All.SendAsync("ReceiveMessage", this.GetAlarmList());
        }
        public string GetAlarmList()
        {
            var AlarmList = new List<string>();
            using (NpgsqlCommand sqlCmd = new NpgsqlCommand())
            {
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.CommandText = "sp_alarm_speed_process_get";
                NpgsqlConnection conn = new NpgsqlConnection(cs);
                conn.Open();
                sqlCmd.Connection = conn;
                using (NpgsqlDataReader reader = sqlCmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        NotificationModel model = new NotificationModel();
                        
                    // you must fill  your model items
                        AlarmList.Add(model.Bericht);
                    }
                    reader.Close();
                    conn.Close();
                }



            }
            _cache.Set("SpeedAlarm", SerializeObjectToJson(AlarmList));
            return _cache.Get("SpeedAlarm").ToString();
        }
        public String SerializeObjectToJson(Object alarmspeed)
        {
            try
            {
                var jss = new JavaScriptSerializer();
                return  jss.Serialize(alarmspeed);
            }
            catch (Exception) { return null; }
        }
        private void conn_StateChange(object sender, System.Data.StateChangeEventArgs e)
        {

            _hubContext.Clients.All.SendAsync("Current State: " + e.CurrentState.ToString() + " Original State: " + e.OriginalState.ToString(), "connection state changed");
        }
    }
    

}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

‘error CS0103: The name ‘CommandType’ does not exist in the current context’

To fix this error, you can try to add:

using System.Data;

‘error CS0246: The type or namespace name ‘JavaScriptSerializer’ could not be found’

For JSON serialization in ASP.NET Core applications, as @Marshal mentioned in comment, Newtonsoft.Json could be used.

And since .NET Core 3, the default JSON serializer for ASP.NET Core is now System.Text.Json that would be higher performance than Newtonsoft.Json.

return System.Text.Json.JsonSerializer.Serialize(alarmspeed);

Note: System.Text.Json may not provide complete parity with Newtonsoft.Json features that list in following doc, you can check it for detailed information.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-core-3-1#table-of-differences-between-newtonsoftjson-and-systemtextjson


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x