The view is also packaged as a resource to be published along with the Assembly in MVC

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.