Skip to content

Instantly share code, notes, and snippets.

@stevebauman
Last active December 18, 2019 16:35
Show Gist options
  • Save stevebauman/b02fe25e4811268d4261f3d941ced5ff to your computer and use it in GitHub Desktop.
Save stevebauman/b02fe25e4811268d4261f3d941ced5ff to your computer and use it in GitHub Desktop.
Laravel Mix manifest helper for ASP MVC apps (C#)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="@Mix.Get("/css/app.css")" />
</head>
<body>
@RenderBody()
</body>
</html>
using System;
using System.IO;
using System.Web;
using System.Web.Helpers;
namespace Helpers
{
/// <summary>
/// Helper class for parsing the Laravel Mix manifest.
/// </summary>
public static class Mix
{
/// <summary>
/// The manifest file name.
/// </summary>
private static string Manifest = "mix-manifest.json";
/// <summary>
/// Returns the versioned file path of the given asset.
///
/// Returns the given path if the given is not found in the manifest.
/// </summary>
/// <param name="path">string</param>
/// <returns>string</returns>
public static string Get(string path)
{
try {
string fullPath = String.Format("{0}/{1}", HttpContext.Current.Server.MapPath("~"), Manifest);
string content = File.ReadAllText(fullPath);
return Json.Decode(content)[path] ?? path;
} catch (Exception e) {
// Do something with the exception.
}
return path;
}
}
}
@stevebauman
Copy link
Author

This allows you to use Laravel Mix's versioning via mix.version().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment