Make your web programs "dynamic ., Web programs

Source: Internet
Author: User

Make your web programs "dynamic ., Web programs

Here, you may ask how the asp.net program is a dynamic website?

What I call dynamic means dynamic loading and dynamic updating. Well, you may have to ask whether a dynamic website is dynamically loaded or updated. The asp.net program is attached to IIS (not the only one of them) and has code updates. You only need to copy the program to the corresponding directory. Refresh the interface and load the latest program. You do not need to restart IIS.

But here I am talking about dynamic loading scripts. The script here is a C # class file (*. cs) file that is dynamically compiled and added to the memory running instance.

Someone may ask, what is the need for this ????

If the program has been modified, modify-compile-copy. Is the latest program, does not affect online programs. But think about it. What if it's a small problem? If you change the code because of different requirements, you can make minor changes to the Code. So it is necessary to compile the code and upload the file?

Or the computer around you does not have any compilation tools, but you need to modify some functions of the program. What should you do if it is within the permitted range?

Maybe you say this is boring. No need, no use case ~! ,

Well, I can only say that you haven't met anyone. Ask the people around you who manage and release the program deployment. What are their difficulties? Every small bug update, it is very troublesome to copy multiple files and upload them to the server.

Okay. Let me talk about why I want to write this article. What happened to me.

During this time, I revised a portal website for a company. The front-end display page must be a static page.

The pure static page requires that after the website content is updated in the background, you need to publish and generate a pure Static Page and put it under the website directory for access ~!

The technology I use is to generate a content page through a template file and save the file.

However, the template file contains some content that needs to be constructed in the C # code and then replaced with the template file content.

C # The HTML built in it may contain tags or css styles. It is a common task to update a problem or change a requirement. But once there is an update, it is a headache to modify the code and release it. I have described it before.

In the previous article, I developed the Game server step by step (iii) Load scripts and server hot updates (II) the full version introduced if the script file is loaded, however, there was a problem in the previous test, but it was not tested to run in asp.net.

The problem lies in reading the referenced assembly loaded by the program.

Previously, when querying the referenced assembly, I found the Assembly referenced under the referenced assembly. As a result, some files that cannot be loaded are loaded in IIS,

            var asss = AppDomain.CurrentDomain.GetAssemblies();            foreach (var item in asss)            {                foreach (var item222 in item.GetModules(false))                {                    ddlNames.Add(item222.FullyQualifiedName);                }            }

You need to find the Assembly referenced in the referenced assembly.

            var asss = AppDomain.CurrentDomain.GetAssemblies();            foreach (var item in asss)            {                if (!item.ManifestModule.IsResource() && item.ManifestModule.FullyQualifiedName.EndsWith(".dll") || item.ManifestModule.FullyQualifiedName.EndsWith(".DLL"))                {                    ddlNames.Add(item.ManifestModule.FullyQualifiedName);                }            }

 

Create an IScript folder and an ICreateScript. cs interface file.

namespace TestWebLoadScript.IScripts{    public interface ICreateScript : IBaseScript    {        void CreateHtml(Page page, string type);    }}

As described in the previous article, I have defined the script loader. I don't know which classes you have loaded. Because the script class does not know the class name and instance, I use the interface to control it, it can also be seen as interface programming.

 

Next, create a Scripts folder, create a PC file, and then create the Default folder below

Create an Indexhtml. temp File and store the template code.

<! DOCTYPE html> 

The above <% string %> is a replacement keyword.

Create a CreateIndexScript. cs file to create an html file.

Namespace TestWebLoadScript. scripts. PC. default {public class CreateIndexScript: ICreateScript {string filePath = "C:/html/PC/index.html"; public void CreateHtml (System. web. UI. page page, string type) {if (type = "Default") {if (! System. IO. directory. exists (System. IO. path. getDirectoryName (filePath) {System. IO. directory. createDirectory (System. IO. path. getDirectoryName (filePath);} string htmlPath = page. server. mapPath ("/Scripts/PC/Default/Indexhtml. temp "); string htmlContext = System. IO. file. readAllText (htmlPath); using (System. IO. streamWriter sw = System. IO. file. createText (filePath) {sw. write (htmlContext. replace ("<% string %>", "I am Default" + DateTime. now. toString ("yyyy-MM-dd HH: mm: ss: fff ")));}}}}}

Create an index. aspx page

namespace TestWebLoadScript{    public partial class index : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            string savePath = Server.MapPath("/") + System.Configuration.ConfigurationManager.AppSettings["scriptPath"].ToString();            Sz.Network.LoadScriptPool.LoadScriptManager.GetInstance.LoadCSharpFile(new string[] { savePath });            var scripts = Sz.Network.LoadScriptPool.LoadScriptManager.GetInstance.GetInstances<ICreateScript>();            string type = this.Request["type"];            foreach (var script in scripts)            {                script.CreateHtml(this, type);            }        }    }}

This is our current program requirement. The HTML template for generating the index

At this time, the web program is released.

But the demand suddenly came up, or the template was changed. If our HTML and template generation programs are written to the code, then we must change the code at this time ,. Then compile ., Release again. Generate the latest template page based on the data;

Very troublesome.

However, after a script is generated,

Copy the two files, modify them, and replace them

Qq version

Of course, you need to modify the script file namespace.

namespace TestWebLoadScript.Scripts.PC.QQ

The static page of the QQ version is generated.

Copy the same code to create the Mobile version;

The static page of each version is generated.

I don't know what the audience members need to talk about ???? Please leave a message ~!

 

Related Article

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.