Last active
December 18, 2019 16:35
-
-
Save stevebauman/b02fe25e4811268d4261f3d941ced5ff to your computer and use it in GitHub Desktop.
Laravel Mix manifest helper for ASP MVC apps (C#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<link rel="stylesheet" type="text/css" href="@Mix.Get("/css/app.css")" /> | |
</head> | |
<body> | |
@RenderBody() | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This allows you to use Laravel Mix's versioning via
mix.version()
.