question

Andrus-0543 avatar image
0 Votes"
Andrus-0543 asked Bruce-SqlWork edited

Porting MVC4 offline application

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.



dotnet-aspnet-core-mvcdotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork edited
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.