Open from a simple asp.net 5 site. NET Cross-platform tour

Source: Internet
Author: User
Tags format object exit end net string static class access








After experiencing the "Black 1 seconds" of air delight in Ali's cloud, we are "forced" to consider implementation. NET, replacing the Web server from Windows with Linux. And this "forced" in a long-standing desire to become the case. The wish is--"Mac writes. NET programs, Linux runs. NET programs."



Since the water has arrived, the canal has become, then we wait for what, set out to leave.



Today we take our first step--a very simple asp.net 5/mvc 6 site that is deployed on Linux based on DNX/COREFX/CORECLR--Announces. NET cross-platform journey "opened!"



This is based on Cross-platform. NET site has been online, access to the Web site: http://about.cnblogs.com/.



The site is deployed on the CentOS server (the deployment step), the server has only DNX installed, and mono is not installed, so it is completely based on. NET Core. The back-end Web server is Kestrel and is currently cross-platform. NET is the only Web server available on a non-Windows platform.



The CentOS server runs as follows:


[Root@about-server aboutus]# Dnx. Kestrel
started


The front-end Web server is Aliyun SLB (load Balancing), if not SLB, you can use Nginx to do the reverse proxy directly on the CentOS. Why use a front-end Web server? Because the Kestrel Web server is so primitive that it doesn't even have the keep-alive and HTTP compression functions.



The site's ASP.net 5 program was developed with VIM on the Ubuntu server.



The project file structure is as follows:


 


.
├── Controllers
│   ├── AboutController.cs
│   └── HomeController.cs
├── Extensions
│   └── HtmlHelperExtensions.cs
├── project.json
├── project.lock.json
├── Startup.cs
├── Views
│   ├── About
│   │   ├── Ad.cshtml
│   │   ├── Contact.cshtml
│   │   ├── Intro.cshtml
│   │   └── Job.cshtml
│   ├── Shared
│   │   └── _Layout.cshtml
│   └── _ViewStart.cshtml
└── wwwroot
    ├── images
    │   ├── about_cnbogs.gif
    │   ├── icon_arrow.gif
    │   └── icon_triangle.gif
    └── styles
        └── about.css


Configuration in the Project.json file:


  


{
    "webroot": "wwwroot",
    "exclude": ["wwwroot"],
    "commands":{
        "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:8001"
    },
    "dependencies":{
        "Kestrel": "1.0.0-*",
        "Microsoft.AspNet.Mvc": "6.0.0-*",
        "Microsoft.AspNet.StaticFiles": "1.0.0-*",
        "Microsoft.AspNet.Diagnostics": "1.0.0-*"
    },
    "frameworks":{
        "dnxcore50": {}
    }
}


Frameworks only Dnxcore50, stating that the program is entirely based on. NET Core. However, because the CoreCLR dnu Restore feature is currently unavailable, it is necessary to install mono in the development environment and install DNU packages with Retore NuGet based on Mono.



The code in Startup.cs is as follows:


 


using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;

namespace CNBlogs.AbouUs.Web
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseErrorPage();

            app.UseMvcWithDefaultRoute();

            app.UseStaticFiles();
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }
    }
} 

(Note: Project.json and Startup.cs do not have redundant configuration and code)



The program is very simple, there is no database operation, the main is to display text content. Slightly more complicated is a htmlhelpder extension method (the code is ported from an existing project), which automatically highlights the Navigation tab on the left-hand side based on the access URL, as follows:


using Microsoft.AspNet.Mvc.Razor;
using Microsoft.AspNet.Mvc.Rendering;

namespace Microsoft.AspNet.Mvc.Rendering
{
    public static class HtmlHelperExtensions
    {
        public static HtmlString TabLink(this IHtmlHelper htmlHelper, string linkText, string linkUrl, string viewName)
        {
            var view = htmlHelper.ViewContext.View as RazorView;
            if (view != null && view.Path.IndexOf("/" + viewName + ".", System.StringComparison.OrdinalIgnoreCase) > -1)
            {
                return htmlHelper.Raw(string.Format("<a href=\"{0}\" class=\"current\">{1}</a>", linkUrl, linkText));
            }
            else
            {
                return htmlHelper.Raw(string.Format("<a href=\"{0}\">{1}</a>", linkUrl, linkText));
            }
        }
    }
}

The code for this ASP.net 5 program is a step-by-step process from scratch with vim (in addition to views and htmlhelperextensions), from which a more profound understanding of the ASP.net 5 of some of the working principles, so also get a run this simple asp.net Minimum configuration required for 5 programs.



The most painful in the development process is to modify the code after the ASP.net 5 will not automatically recompile, you need to DNX run the program, and Kestrel currently has a bug, unable to exit, even if the ssh window is closed, but also run, must use unconventional means to force the end of the process (PS all; kill-9 [PID]). But kestrel this bug but bring a surprising side effect, just because it started on the run, how will not quit, equivalent to a background service in the way of operation, all of a sudden resolved the deployment of how the background run asp.net 5 site problem.



Although just a very simple asp.net 5 program, though just. NET Cross-platform journey is a very, very small step, but it is an important step because it gives us a real sense of ——. NET across the platform, the road at the foot.



Update



Around 15:35, an exception caused Kestrel exit, rerun Dnx after the normal return. The exception information is as follows:


Unhandled Exception:System.NullReferenceException:Object reference not set to a instance of an Object.
   At Microsoft.AspNet.Server.Kestrel.Networking.UvShutdownReq.UvShutdownCb (IntPtr ptr, Int32 status)







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.