Porting MVC4 offline application

Andrus 121 Reputation points
2021-09-07T09:35:36.113+00:00

Trying to port ASP.NET MVC4 HTML offline application to .NET 5.
It should allow to enter order without internet connenction and send it to MVC 5 controller over internet if connection is present.

It has manifest controller

namespace Store.Controllers
{
    public class MobileOrderController : ControllerBase
        {

        public async Task<IActionResult> Manifest()
        {
            return new AppCacheResult(new[] {
                BundleTable.Bundles.ResolveBundleUrl("~/bundles/jquery")
            },
           fingerprint: BundleTable.Bundles
                           .FingerprintsOf("~/bundles/jquery"));
        }
    }
     }

        public class AppCacheResult : IActionResult
        {
            public AppCacheResult(
                IEnumerable<string> cacheAssets,
                IEnumerable<string> networkAssets = null,
                IDictionary<string, string> fallbackAssets = null,
                string fingerprint = null)
            {
                if (cacheAssets == null)
                {
                    throw new ArgumentNullException("cacheAssets");
                }

                CacheAssets = cacheAssets.ToList();

                if (!CacheAssets.Any())
                {
                    throw new ArgumentException(
                        "Cached url cannot be empty.", "cacheAssets");
                }

                NetworkAssets = networkAssets ?? new List<string>();
                FallbackAssets = fallbackAssets ?? new Dictionary<string, string>();
                Fingerprint = fingerprint;
            }

            protected IEnumerable<string> CacheAssets { get; private set; }

            protected IEnumerable<string> NetworkAssets { get; private set; }

            protected IDictionary<string, string> FallbackAssets
            {
                get;
                private set;
            }

            protected string Fingerprint { get; private set; }

            public async Task ExecuteResultAsync(ActionContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }

                var response = context.HttpContext.Response;

                response.Cache.SetMaxAge(TimeSpan.Zero);
                response.ContentType = "text/cache-manifest";
                response.ContentEncoding = Encoding.UTF8; //  needs to be utf-8
                response.Write(GenerateContent());
            }

            protected virtual string GenerateHeader()
            {
                return "CACHE MANIFEST" + Environment.NewLine;
            }

            protected virtual string GenerateFingerprint()
            {
                return string.IsNullOrWhiteSpace(Fingerprint) ?
                    string.Empty :
                    Environment.NewLine +
                    "# " + Fingerprint +
                    Environment.NewLine;
            }

            protected virtual string GenerateCache()
            {
                var result = new StringBuilder();

                result.AppendLine();
                result.AppendLine("CACHE:");
                CacheAssets.ToList().ForEach(a => result.AppendLine(a));

                return result.ToString();
            }

            protected virtual string GenerateNetwork()
            {
                var result = new StringBuilder();

                result.AppendLine();
                result.AppendLine("NETWORK:");

                var networkAssets = NetworkAssets.ToList();

                if (networkAssets.Any())
                {
                    networkAssets.ForEach(a => result.AppendLine(a));
                }
                else
                {
                    result.AppendLine("*");
                }

                return result.ToString();
            }

            protected virtual string GenerateFallback()
            {
                if (!FallbackAssets.Any())
                {
                    return string.Empty;
                }

                var result = new StringBuilder();

                result.AppendLine();
                result.AppendLine("FALLBACK:");

                foreach (var pair in FallbackAssets)
                {
                    result.AppendLine(pair.Key + " " + pair.Value);
                }

                return result.ToString();
            }

            private string GenerateContent()
            {
                var content = new StringBuilder();

                content.Append(GenerateHeader());
                content.Append(GenerateFingerprint());
                content.Append(GenerateCache());
                content.Append(GenerateNetwork());
                content.Append(GenerateFallback());

                var result = content.ToString();

                return result;
            }
        }

This causes compile error in .NET 5 since response.Cache and response.ContentEncoding does not exis in lines

response.Cache.SetMaxAge(TimeSpan.Zero);
response.ContentEncoding = Encoding.UTF8; 

Also BundleTable.Bundles does not exist in .NET 5

How to convert it to .NET 5 ?
Or is there better way to create HTML offline application in .NET 5 using ASP.NET MVC Core.

Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-09-07T14:45:36.407+00:00
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.