With the extensibility of ASP. NET MVC, we can redefine the view engine and package The view file *.cshtml into the assembly as well.
This improves the independence of the MVC framework-based assemblies and simplifies deployment.
Preferred to redefine a view engine:
The source code is as follows:
namespace helpers{//<summary>///For standard razorviewengine extensions so that the view engine can find resources packaged in an assembly as a view///</summary> public class Resviewengine:razorviewengine {///<summary>//Resources may be in the namespace collection///</summa ry> [Inject] public string[] Resnamespaces {get; set;} private static string RootPath = System.Web.Hosting.HostingEnvironment.MapPath ("~"); Private string[] _startwiths = {"~/views/", "~/areas/"}; private string logFile = Path.Combine (RootPath, "App_Data", "ResViews.txt"); Public Resviewengine () {//delete previously written files if (file.exists (logFile)) {St ring[] Filenamestrs = File.readalltext (logFile). Split (new string[] {Environment.NewLine}, stringsplitoptions.removeemptyentries); foreach (string fn in Filenamestrs) {if (File.exists (FN)) FILE.D Elete (FN); } File.delete (LogFile); }} protected override bool FileExists (ControllerContext ControllerContext, string virtualpath) { if (base. FileExists (ControllerContext, virtualpath)) {return true; } if (resnamespaces = = null) return false; foreach (String startwith in _startwiths) {if (!virtualpath.startswith (Startwith)) continue; string tail = virtualpath.substring (startwith.length); tail = tail. Replace ("/", "."); Stream stream = null; foreach (string ns in Resnamespaces) {string NST = ns. Replace (". Controllers "," "); String resname = Nst + startwith.replace ("/", "."). Replace ("~", "") + tail; stream = Assembly.Load (NST). GetManifestResourceStream (Resname); if (stream! = null) break; } if (stream = = null) return false; String path = ControllerContext.HttpContext.Server.MapPath (virtualpath); Iohelper.writestreamtofile (stream, path); Write the file path name into a text file File.appendalltext (logFile, path + Environment.NewLine); } return base. FileExists (ControllerContext, virtualpath); } }}
Of course, there are some help classes that need to be replenished.
It is also necessary to replace the above view engine with the original one in Global.asax.cs:
protected virtual void Application_Start () { arearegistration.registerallareas (); //...... ViewEngines.Engines.Clear (); VIEWENGINES.ENGINES.ADD (New Resviewengine ()); }
Finally, you will need to package the view in VS2013, press F4 to enter the Properties window, build the action, select "Embedded Resource"
The view is also packaged as a resource to be published along with the Assembly in MVC