3

.NET MVC 5

I need to add dynamic text to the start_url in a manifest.json file for a web app, and possibly even dynamic splash screens (but I will be happy with the start_url). The start_url needs to contain a seed that will essentially create a “deep” start_url, ultimately resulting in something like:

{
  "background_color": "black",
   ....
  "start_url": "/ABCDEFG”
}

where “ABCDEFG” is the dynamic text which will be different for different clients. "ABCDEFG" would be delivered server-side, of course.This would eliminate the need for a separate manifest.json files which would be a nightmare.

I didn’t know if there was a way to “stream” the dynamic data into the link or I could probably figure out a way to dynamically create “temp” on the fly like manifest.json.ABCDEFG but that seems clunky and I thought that there might be a more elegant and cleaner way. Sorry if I sound a little vague or I am lacking the correct terminology.

I have viewed "How can I use php to populate a manifest.json file?" which is almost spot on to what I need to do but I don't know how to do this in .NET.

Even a few links directing me to a solution would be GREATLY appreciated. I am coming up otherwise empty on google.

adiga
  • 34,372
  • 9
  • 61
  • 83
HumbleBeginnings
  • 1,009
  • 10
  • 22

1 Answers1

2

I had a similar issue. I resolved it by simply reading in the json file as a string and replacing some placeholders.

Here's my controller (with fake variable names):

[AllowAnonymous]
public class PublicController : Controller
{
  public ActionResult SiteManifest()
  {
    var rawJsonText = System.IO.File.ReadAllText(Path.Combine([MySourceFolder], "rawManifest.json"));
    rawJsonText = rawJsonText.Replace("@url@", [MyRootUrl]);
    rawJsonText = rawJsonText.Replace("@icon@", [MyIconName]);
    return Content(rawJsonText, "application/json");
  }
}

and my redacted rawManifest.json file (which can reside anywhere on your server):

{
  "name": "x",
  "short_name": "x",
  "description": "x",
  "start_url": "@startUrl@",
  "scope": "@startUrl@",
  "display": "standalone",
  "orientation": "landscape-primary",
  "background_color": "x",
  "theme_color": "x",
  "icons": [
    {
      "src": "../images/@icon@144.png",
      "sizes": "144x144",
      "type": "image/png"
    },
    {
      "src": "../images/@icon@192.png",
      "sizes": "192x192",
      "type": "image/png"
    }
  ]
}

I had to ensure that the Action was callable by anonymous users, and it worked great.

Glen Little
  • 6,951
  • 4
  • 46
  • 68
  • 1
    Can you share the full code? i have trouble linking the request of the manifest.json file to the controller. – illeb Oct 10 '18 at 14:19
  • 1
    This is the full code, but where I've used [ ] in the 4 lines of code, you need to put your own content. So, [MySourceFolder] would be "C:\folder" or whatever you need to put. – Glen Little Oct 10 '18 at 19:04