. NET Cross-platform: Follow the error message on your Mac step-by-step handwritten ASP. 5 Program

Source: Internet
Author: User
Tags hosting

Today, when I was in high-speed rail, I tried a dumb way to learn ASP. NET 5, starting with an empty folder and running DNX. Kestrel the error message of the command, step-by-step to write the code until a simplest ASP. NET program is run.

The specific steps to try are as follows.

Create a new empty folder Hellocnblogs:

mkdir Hellocnblogs && CD $_

run the dnx. Kestrel Command (CORECLR-based DNX) in this empty Hellocnblogs folder, and the result is the following error message:

System.InvalidOperationException:Unable to resolve project ' Hellocnblogs ' from/git/hellocnblogs at   Microsoft.Framework.Runtime.ApplicationHostContext. ctor ...

Add an empty Project.json file (command for Touch Project.json) and run DNX. Kestrel command, the error message is as follows:


The JSON file can ' t is deserialized to a JSON object. At Microsoft.Framework.Runtime.Project.GetProjectFromStream (
Stream stream, String projectName, String ProjectPath, ICollection ' 1 diagnostics)

Add {} to the Project.json file and run DNX. Kestrel command, the error message is as follows:

System.InvalidOperationException:Unable to load application or execute command ' Kestrel '. Microsoft.Framework.ApplicationHost.Program.ThrowEntryPointNotfoundException (defaulthost host, String ApplicationName, Exception innerexception)

Add Kestrel command to Project.json:

"Commands": {    "Kestrel": "Microsoft.AspNet.Hosting--server Kestrel--server.urls http://localhost:8080 "}

Run DNX. Kestrel, the error message is as follows:

System.InvalidOperationException:Unable to load application or execute command ' Microsoft.AspNet.Hosting '. Available Commands:kestrel.   At Microsoft.Framework.ApplicationHost.Program.ThrowEntryPointNotfoundException (   defaulthost host, String ApplicationName, Exception innerexception)

Add a reference to Kestrel in Project.json

"Dependencies": {"Kestrel": "1.0.0-*"}

After running Dnu restore (mono-based dnu), run Dnx again. Kestrel, the error message changes to:

System.InvalidOperationException:Failed to resolve the following dependencies for Target framework ' dnxcore,version= v5.0 ':   Kestrel 1.0.0-beta6-11871

Add Frameworks to Project.json:

"Frameworks": {     "Dnxcore50": {}}

Run Dnu restore && dnx. Kestrel, error occurred:

System.invalidoperationexception:a type named ' startupproduction ' or ' Startup ' could not being found in assembly ' Hellocnblo GS '. At Microsoft.AspNet.Hosting.Startup.StartupLoader.FindStartupType (String startupassemblyname, IList ' 1 Diagnosticmessages)

Create the Startup.cs file and add a startup class:

namespace hellocnblogs{    publicclass  Startup    {            }}

Continue to DNX. Kestrel, error occurred:

System.invalidoperationexception:a method named ' configureproduction ' or ' Configure ' in the type ' Hellocnblogs.startup ' Could not being found.   At Microsoft.AspNet.Hosting.Startup.StartupLoader.FindMethod (   Type startuptype, String methodName, String Environmentname, Type ReturnType, Boolean required)

Add the Configure method to the Startup class:

 Public class startup{    publicvoid  Configure (Iapplicationbuilder app)    {    }    }

Continue to DNX. Kestrel, error occurred:

Error cs0246:the type or namespace name ' Iapplicationbuilder ' could not being found (is you missing a using directive or an Assembly reference?)

To add a namespace in Startup.cs:

using Microsoft.AspNet.Builder;

Continue to DNX. Kestrel, this is a successful operation!

Started

Here with the browser to access the http://localhost:8080/, can be successfully accessed, but the page is blank, because we do not have any content output operation in the program.

Then, add the code for the input in the Startup.cs:

 Public class startup{    publicvoid  Configure (Iapplicationbuilder app)    {        app. Run (asyncawait context. Response.writeasync ("Hello, cnblogs! " ));    }    }

Continue to use DNX. Kestrel command Run, error occurred:

Error CS1061: ' HttpResponse ' does not contain a definition for ' writeasync ' and no extension method ' WriteAsync ' accepting A first argument of type ' HttpResponse ' could be found (is the missing a using directive or an assembly reference?)

To add a namespace in Startup.cs:

using Microsoft.AspNet.Http;

Run again, success!

$ DNX. kestrelstarted

The browser accesses the http://localhost:8080/to get the normal response content:

Hello, cnblogs!.

With this trial and error method, you get the minimum configuration for running one of the simplest ASP. NET 5 Programs:

A folder, 2 files (Project.json and Startup.cs).

Content in the Project.json file:

{    "commands": {        "Kestrel": "Microsoft.AspNet.Hosting--server Kestrel--server.urls http:/ /localhost:8080 "    },    " dependencies ": {        " Kestrel ":" 1.0.0-* "    },    "Frameworks": {         "Dnxcore50": {}}    }

Content in Startup.cs:

using microsoft.aspnet.builder;using microsoft.aspnet.http;namespace hellocnblogs{public    class Startup    {        void  Configure (Iapplicationbuilder app)        {            = = await context. Response.writeasync ("Hello, cnblogs!" ));        }        }}

Although it is the most stupid way to write a simple program, but so hands-on operation, the feeling is not the same.

. NET Cross-platform: Follow the error message on your Mac step-by-step handwritten ASP. 5 Program

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.