In the last collection, we deployed our first ASP. NET application in Mono. In addition, we also integrated the PostgreSQL database to implement a small site for simple CRUD operations. Their successful deployment and normal operation reflect the feasibility of. NET in Linux.
At the same time, as mentioned in the end of the last round, we will discuss how to deploy and use the enterprise-level open-source framework Spring. NET in Mono. However, due to my personal affairs, I have never had time to systematically study Spring. NET. Here, I apologize to you. Therefore, the content we will discuss in this round will change, and we will not discuss "Srping. NET "how to deploy in Mono, we will discuss how to learn the" ASP. net mvc "how to deploy the sample code to our Mono. You can click here to download the sample code (because the code is too large to be uploaded to the garden, so I put it on a micro disk. I hope you will forgive me ).
In this round, we will discuss and learn:
1. Preparations before deployment
2. Deploy the MVC3 website application to Mono from scratch
3. Inspired by MVC3 deployment, MVC4 is deployed to Mono.
4. Another MVC4 deployment method that is "Opposite" to the previous deployment Method
1. system conventions-preparations before deployment
Recently, many of my friends have posted a blog post saying that they have successfully deployed the latest MVC Framework in Mono. What is MVC? I think you may know that as an important website construction model launched by Microsoft, MVC is equivalent to WebForm and its importance cannot be ignored. Imagine that if MVC cannot be deployed in Mono, our Linux. NET seems to be a broken-wing bird-"whether it can fly or not? "No, it just cannot fly. Therefore, it seems that the normal running of MVC in Mono is a "of course" for Linux. NET.
Let's get started. before deploying MVC, we need to create a Jexus website Configuration:
Create a new folder for storing files in Linux, then cd (enter) to the Jexus directory for storing website configuration files, and then create a new website configuration file "linux. net5" for this experiment.
Add the content we configured
Save and exit, and then restart our Jeuxs.
Here, it is worth noting that:
This is the "NoFile" configuration item. You must disable this function (comment out the function after adding it or not add it directly. Jexus is disabled by default ). In MVC, our access path is overwritten by a route. When we initiate an access request, such as localhost/Home/Index/, the request will be matched and rewritten by the route, finally, the request is assigned to HomeController in the Controller folder. the Index method in the cs file, which then processes the request and sends a response. In the process of processing the entire MVC "request-response", URl requests initiated by the browser in the address bar may not always be real file physical address access requests on the website. On the contrary, in the NoFile detection of Jexus, Jexus checks the physical address of the file for the access request (URL) initiated by the user, when the detection fails, a preset response page (such as the 404 page) is directly responded to the user.. NET does more processing on the URl. Therefore, the request will never be assigned to the correct Action for processing, and the entire MVC website will be decommissioned.
Similarly, for some. for websites that use URlRewrite in. NET, the "NoFile" function of Jexus also needs to be disabled. The reason is the same as that of MVC, the request that will be overwritten by the URL has not passed the Jexus mark and is rejected.. NET, ).
2. Deploy the MVC3 website application
Deploying the MVC3 application in Mono is not the ultimate goal of this round, but it is true that the most important content of this round is as long as you have learned how to deploy MVC3 in Mono, deploying MVC4 is the same thing. In fact, the successful deployment of MVC3 in Mono is not a new thing. There have been many successful cases earlier. There are also some articles about how to deploy MVC3 in Mono, if you are interested, you can refer to it on your own. Of course, you can also find a method directly from this article. Here we use the Step By Step method for discussion and learning, step by step, describe how to perform the operation, and point out the points that need attention and the reasons for parsing.
Okay. First create an MVC3 application "MVC31" and select "Empty template" and "Razor view engine".
Since this is an empty template and there is no ready-made Controller in it, we simply create a HomeController and add the previous Index Action and related views. Then run:
There is no problem, but if we release it to Linux, the effect will be different:
That's right. As you may think, it cannot run normally. Follow the prompts above to set the "CustomError" node, set it to "OFF", and re-release the node to see if something went wrong:
The first problem occurs. Obviously, it is caused by the Entity Framework. If you have carefully read the previous article "Linux. NET learning note (4)", you will probably understand the reasons and solutions. Just as why I used PetaPoco instead of EF (my favorite ORM) in the last round, the EF version in Mono is already 6.0 and does not support EF frameworks earlier than this version. It happens that the EF version officially released by Microsoft is only 5.0 at most, this leads to our first error. However, the problem here is temporary. With the release of Microsoft EF6.0 in the future, this problem will be solved.
We use NuGet to uninstall all Entity frameworks.
Then, clear some residual items in Web. Config and Global, and release them again.
Then we went smoothly to our second problem. Here I will explain the cause of this problem. Because mono 3.0.X has not yet supported Chinese Language and Culture (2. therefore, the system uses "zh-CN" by default to make Mono unidentifiable. The solution to this problem is to add a "globalization" node to the "system. web" node of WebConfig and set "uiCulture" to "en-US". The recommended configuration here is "<globalization culture =" zh-CN "uiCulture =" en-US "/>", in this way, our program itself can run in "English" syntax, but it can be displayed in "Chinese" syntax.
We add this node and release it again:
Unfortunately, we continue to make a new mistake. How do you feel here? Crazy? Think about giving up? As the saying goes: "half a ninety-Year-Old Man in the industry", don't give up, because success is in front of you. By reading a lot of information from the Internet, we can find the culprit of this problem. That's right, it's Microsoft. web. infrastructure. dll ", we set Windows to" Microsoft. web. infrastructure. dll "and" Microsoft. web. infrastructure. dll "decompilation:
We can see that the Infrastructure in Windows and the Infrastructure in Mono are not the same thing. This is easy to handle. We will remove the Infrastructure in the MVC3 project and let the program use the dynamic library that comes with Mono, it may solve our existing problems. After deleting this dynamic library, we will release it again:
Finally we can see the "Index" we 've been trying to see. MVC3 successfully ran in Mono.
Taking advantage of the hype, we combined the PostgreSQL database and PetaPoco to quickly create a small CRUD application.
First, create a new product table "Goods". Its table structure is as follows:
Then, you can create related controllers, models, and views. Here, we will not describe how to write and implement the Code. If you are interested, you can View it in the Code demonstration.
Publish the website to Mono:
The page can be displayed normally, no problem. Let's try to add a new product:
An error is reported when you add a product, and an assembly cannot be found in "Details.
Through troubleshooting, we found that a DLL file we referenced does not exist in Mono:
In this way, you only need to find "System. componentModel. dataAnnotations. dll "(pay attention to the version), and then manually add it to the bin directory of our released website.
Add data again. OK! Added successfully!
The "Linux. NET learning notes" product appeared. Now, the simple deployment of MVC3 has been completed ~!!
Here are some tips:
(1) Because I use a virtual machine, I have integrated a Linux Samba service for direct release, readers can also publish the website through Ftp or upload it locally. Remember to restart Jexus after the release.
(2) The "Infrastructure" dynamic library can be processed in addition to the press conference deletion method, or the "do not copy" attribute can be used for processing.
(3) readers can also refer to the article "key points for running MVC4 and WebApi on mono3.0.10 + Jexus5.3". The above method is the same as here.
3. Deploy MVC4 in Mono
In the previous section, we had a hard time "getting truth" and successfully deployed an MVC3 website application to Mono. After this success, we continue to advance towards our ultimate goal in this round-deploying an MVC4 application in Mono. In fact, both MVC3 and MVC4 have similar deployment methods. In the previous section, we will detail the MVC3 deployment to serve as a reference for deploying MVC4.
All right, it's better to think about it. We will immediately create an MVC4 website application:
Then, based on the previous steps of deploying MVC3, make the following modifications:
(1) Remove all Entity Framework applications (use NuGet to uninstall EF and clear all EF residues)
(2) Add "<globalization culture =" zh-CN "uiCulture =" en-US "/>" to the "system. Web" node in the web. Conf configuration file"
(3) Remove the dynamic library file "Infrastructure ".
Publish this MVC4 application to Mono:
It can run normally. Similarly, we can add, query, modify, and delete a "product" and add the missing "System. ComponentModel. DataAnnotations. dll:
The functions we want are also implemented.
Through the "image processing" method, we successfully deployed an MVC4 website application to Mono, which is almost the same.
4. Another version of MVC4 deployment Method
From the beginning of this round, whether to deploy MVC3 or MVC4, the methods we adopt are to directly use the MVC template provided by Visual Studio, then, you can use the "Where can't delete" method to delete or modify a region that is not supported or cannot be directly compatible with Mono. We can say that we adopt a "reverse" method to obtain a MVC application supported by Mono. Speaking of this, readers may have guessed, "Since there is such a reverse method, there should also be a positive way to deploy it ". Recently, I have read a question titled "try to run ASP under Mono 3.0. net mvc 4 was inspired by some articles and found that there are still some readers who say "positive" methods to obtain Mono support for MVC4 applications. Instead of using the MVC template provided by Visual Studio, You can manually build an MVC application by adding necessary MVC library files to Web applications.
In this section, we will try to publish a manual method to obtain an MVC4 website application supported by Mono. By referring to the article, we will deploy the MVC4 application in Mono based on our "reverse" deployment methods and experience.
First, create an empty Web application (note, not an MVC4 application ):
Then, install the MVC4 library file through the NuGet management tool:
Then, manually create the corresponding folders and files:
Several files and folders must be created:
(1) Controller Folder: used to store Controllers
(2) Views Folder: used to store templates
(3) Models Folder: used to store object-class files
(4) Global. asax file: used to initialize an application at startup (register a route, etc)
Add the configuration required for MVC4 to the website configuration file:
Remove the "Infrastructure" dynamic library file that will "blame" in Mono, and then register the route in the "" method of the global configuration file:
Application_Start( { controller = , action = , id =
Manually create controllers and templates in the Controller and View (this is not detailed here, you only need to inherit the corresponding MVC class) and then release them to Mono:
The Controller cannot find the page. Open the released directory:
Our Views folder is missing. In fact, this is because we have created a WebForm application. During the release, our page will be embedded in the bin directory assembly, therefore, the MVC template parsing engine cannot find the VIews directory in the root directory and all templates in it. The solution to this file is also relatively simple. You only need to copy the entire view folder in Visual Studio to the root directory of the website.
Access again:
OK ~!! Normal access!
Here I want to declare that the HttpRuntime of Mono supports targetFramework, so we have not removed the "targetFramework" here (which is different from the reference article). Please pay attention to it ~!!
In this round, we attempted to successfully deploy the MVC3 website application to Mono, and introduced the MVC4 deployment method according to the MVC3 deployment method, in addition, we also introduced how to upgrade Web applications from our existing deployment method to MVC4 applications. I hope that after reading the full text, you will be able to do it yourself. "There is no difficulty in the world, but you are afraid of people with heart." As long as you have perseverance, all the difficulties will not be difficult in the end. At the same time, if you have some better deployment ideas or have any comments or suggestions on this article, please leave a message to share and correct.
Okay, this round is now here. I will not give a notice of the next round, but I have not thought about what to write in the next round. OK. Let's see you later.