Objective
58HouseSearch This project was originally written based on ASP. NET MVC 4, the development environment is WINDOWS+VS2015, the release platform is Linux+mono+jexus, so it seems that the whole project has met the requirements of cross-platform.
In this way, originally I did not have the motivation to do the migration, good things idle profession migration it is nothing.
But isn't it a national day? The poor are not at home poor tour of the world? So... Really a little idle to do nothing.
Discussion on the feasibility of migration
Before the project is migrated, let's discuss the feasibility of the migration first. Why do we have to do a feasibility study? The reason is. NET Core is a cross-platform framework, and the previous generation. NET exists incompatible.
Personally, the main problems with migration are: Code incompatibility, Class library incompatibility, heavy reliance on Windows APIs, or COM components.
Code not compatible
Code incompatibility is not really a hassle. After all, the code is alive, you and I are living, not just a change of words. Take the time to change slowly, always can be fixed.
Class Library not compatible
Either discard it or find a substitute.
Heavily dependent on Windows APIs or COM components
Amount Find a replacement, and can not find the alternative. Give it up, don't think about migrating this project.
The story tells us that when it comes to cross-platform projects, use less of the system API or build.
Back to the 58HouseSearch project.
The code for this project is basically written by me, so there's nothing wrong with rewriting the code.
There are several classes of dependent libraries:
Anglesharp
Newtonsoft.json
Log4net
Anglesharp is a class library used to parse HTML, and it is very refreshing to use LINQ to manipulate HTML.
If the goods can't run on. NET core, I should give up immediately.
However, this really gives the force ...
Newtonsoft.json
In this project is mainly used to record the PV data, non-core functions, optional. But looking at the introduction to NuGet, it also supports. NET Core.
The rest of log4net ... Well, it doesn't support log4net. But this is more of a non-core content, directly lost.
PS: Consider adding nlog to replace log4net later.
As for the reliance on Windows API and the like, in this project is basically not, so skip ...
Preparatory work
- Visual Studio Community with Update 3–free
- . NET Core SDK
- . NET Core
- . NET Core 1.0.1-vs Tooling Preview 2
Friendly tips:
- Visual Studio Community with Update 3 to download the image to install.
The error actions are as follows:
Correct opening method:
Install the. NET Core SDK and. NET core after you install the. NET core 1.0.1-vs tooling Preview 2
Install the. NET Core 1.0.1-vs Tooling Preview 2 This shipment may error 0x80072f8a unspecified errors
Solutions See:
See Links: Installation dotnetcore.1.0.1-vs2015tools.preview2.0.2 0x80072f8a Unspecified error
Once it's all done, you can theoretically see the template for ASP. NET core in the vs2o15-new project. Such as:
Project migration new blank ASP. NET Core Project
When you are new, as follows:
NuGet Get Reference
https://www.nuget.org/packages/AngleSharp/
Https://www.nuget.org/packages/Newtonsoft.Json
Add Controllers Folder
Then copy the previous project's controllers, break the namespace, remove the useless code, and add the appropriate reference.
Add Views Folder
This project directly copies the views of the previous project is completely no problem.
Static file Processing
The file structure in ASP. NET core MVC is slightly different from the file structure of ASP.
The file corresponding to "Img/little/palegreen.png" in the view of ASP. NET core MVC corresponds to "project Path/webroot/img/little/palegreen.png";
in ASP. NET MVC, the corresponding path is "Project/img/little/palegreen.png".
Thus, all of our static files should be placed under the: Webroot folder.
After the above is done, the project structure is as follows:
The next step is to change the code.
Code Migration Startup.cs Adding MVC
123456789101112131415161718192021222324252627282930313233343536 |
public class Startup {//This method gets called by the runtime. Use this method to add services to the container. //For more information in how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void configureservices (iservicecollection services) {//Add MVC frameworkServices. Addmvc ();}//This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure (iapplicationbuilder app, Ihostingenvironment env, Iloggerfactory loggerfactory) {loggerfactory. Addconsole ();if (env. Isdevelopment ()){app. Usedeveloperexceptionpage ();}//Enable static file middlewareApp. Usestaticfiles (); //start MVC routeApp. Usemvcwithdefaultroute (); //Set default page app. USEMVC (routes = {routes. MapRoute ( Name: "Default", Template: "{controller=house}/{action=index}/{id}"); }); }} |
Rewriting the Gethtmlbyurl method
The previous method:
The. NET core rewritten HttpWebRequest and became WebRequest, so the above code was wasted.
Rewrite as follows:
1234567891011121314151617181920212223 |
public staticstring Gethtmlbyurl (string Url, string type = "UTF-8") {try {Url = Url.tolower ();System.Net.WebRequest wReq = System.Net.WebRequest. Create (URL); //Get The response instance. System.Net.WebResponse wresp = Wreq.getresponseasync (). Result; System.IO.Stream Respstream = Wresp.getresponsestream ();using (System.IO.StreamReader reader = new System.IO. StreamReader (Respstream, Encoding.GetEncoding (type ))){return reader. ReadToEnd ();}} catch (System.Exception ex){return string. Empty; } } |
Rewriting the controller code
Well, changed the namespace, the other sentence has not changed directly to pull over ... Skip over.
Post to Ubuntu
Install for Ubuntu 14.04, 16.04 & Linux Mint 17
The first step
12345678910 |
//ubuntu 14.04/linux MintSudosh-c ' echo ' Deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/trusty main ">/etc/ Apt/sources. list. D/dotnetdev. list ' sudo apt-key adv--keyserver apt-mo.trafficmanager.Net--recv-keys 417a0893sudo apt-getUpdate//ubuntu 16.04Sudosh-c ' echo ' Deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/xenial main ">/etc/ Apt/sources. list. D/dotnetdev. list ' sudo apt-key adv--keyserver apt-mo.trafficmanager.Net--recv-keys 417a0893sudo apt-getUpdate |
Step Two
1 |
sudo apt-get install Dotnet-dev-1.0. 0-preview2-003131 |
After the installation, the input dotnet-v should be able to see the version information, such as:
In this case, a sentence completed the Ubuntu run ASP. NET core environment.
Project.json inside the hidden pit dependencies
NET Core 1.0.1-vs Tooling Preview 2 template is inconsistent with the version of ASP.
According to the tutorial Microsoft Dad gave, we installed the. NET Core 1.0.0 on Ubuntu, see.
However, the template we created for the project is. NET Core 1.0.1, see:
What to do? Either upgrade Ubuntu's ASP. NET core or downgrade it.
Because I didn't find the. NET Core 1.0.1 Ubuntu installation package, I chose to downgrade to. NET Core 1.0.0.
Which need to Microsoft.NETCore.App version, Microsoft.AspNetCore.Server.Kestrel, MICROSOFT.ASPNETCORE.MVC These three nodes are changed to "1.0.0". As follows:
1234567891011121314 |
"dependencies": { "Microsoft.NETCore.App": { "version": "1.0.1", "type": "platform" }," Microsoft.AspNetCore.Diagnostics": "1.0.0", " Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", " Microsoft.AspNetCore.Server.Kestrel": "1.0.1", " Microsoft.Extensions.Logging.Console": "1.0.0", " MICROSOFT.ASPNETCORE.MVC": "1.0.1", " Microsoft.AspNetCore.StaticFiles": "1.0.0", " Newtonsoft.json": "9.0.1", " anglesharp": "0.9.8.1 "}, |
Publishoptions
Publish output includes Views folder
1234567 |
"Publishoptions": { "include": [ "Wwwroot", "Web. config", "views" ]}, |
Runtimes
Runtimes is configured as a template to run the platform.
For more information, see Link: Https://docs.nuget.org/ndocs/schema/project.json
1 |
"Runtimes": { "ubuntu.14.04-x64": {}} |
When it's all done, run for a look like this:
123 |
dotnet Restore dotnet run |
A request to see:
Jexus Forwarding/Reverse Proxy
ASP. NET Core "full release, comes with runtime" to Jexus
58HouseSearch projects migrated to ASP. NET Core