Get started with ASP. 5 (1)-Build and develop an ASP. NET 5 Project

Source: Internet
Author: User

Original: ASP. 5 Get Started (1)-Build and develop ASP. NET 5 Project

Asp. NET Get Started (1)-Build and develop an ASP 5 project

ASP. 5 Understanding and Getting Started

Using a custom configuration file

Build a project

First of all, currently only VS 2015 supports the development of the latest ASP. NET 5 programs, so we first need to download and install their latest RC version.

Https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs.aspx

After the installation is complete, open and create a new Web application project:

Then select the ASP. NET 5 empty template in the latter interface:

Here are 2 points of note:

    1. To select the following ASP. NET 5 template, instead of the 4.6 template above.
    2. Choosing the empty template instead of the more complete Web site template is what you want to be able to break down this latest framework system from scratch.

After the establishment of the project, we can see the following project structure:

where Global.json records some of the configuration of the solution, Project_readme.html is an introductory page.

The more important files or folders are the following 3:

L Startup.cs: Start class

L Project.json: Project configuration file

L Wwwroot: The static file directory of the entire webapp (because the minimum template does not load the static file middleware, so the contents of the directory is temporarily invalid, this is explained later)

Start class

The startup class for ASP. NET 5 has only one, and there is no familiar startup file like Global.asax,routeconfig, but this Startup.cs is the only entry for ASP.

In general, the startup class will contain 3 main functions: constructors, configureservices, and configure

Under the minimum template, we only see the following 2 functions, no constructors, although not necessary, but the constructor in the context of reading initialization parameters is also very important, this we will introduce later.

We can understand how these 3 functions work:

constructor : Loads various configuration files and configuration information.

configureservices: Load a variety of services based on dependency injection (Dependency injection), here, in ASP. NET 5, the dependency injection technology is fully used, greatly optimizing the structure of the code.

Configure: Loading the various required middleware (middleware), this concept is consistent with the Owin middleware concept, and is very similar to the original HttpModule in IIS.

Configuration file

The net 5 built-in standard config file has only one: Project.json. Other configuration files are customized and loaded on their own. Of course, there is a Global.json file in the solution directory, but this has little to do with the ASP. NET 5 main project. In addition under the Project.json, also hides a project.lock.json, its essence content is to record a lot. NET runtime Environment (DNX), this file is managed by VS itself, so this file is also being skipped first.

about. NET operating Environment (DNX) It's vs . In order to adapt to the new concept of multi-platform operating environment, This concept is for understanding ASP . compiling and running the environment is critical, However, this content is not within the scope of this article, If you want to understand deeply, please pass my last blog post ASP. NET 5 (VNext) Understanding and Overview provided in the hyperlinks to further study.

So we're getting into Project.json.

"Webroot": "Wwwroot",

"Version": "1.0.0-*",

The former defines the root directory of the WebApp static file, which generally does not need to be modified; the latter defines the version number of the entire project.

"Dependencies": {

"Microsoft.AspNet.Server.IIS": "1.0.0-beta4",

"Microsoft.AspNet.Server.WebListener": "1.0.0-beta4"

},

Apart from. NET runtime Environment Dnx built-in components, there are currently only 2 server components required to load the project: IIS and the console, 2 components that allow the current WebApp to run in IIS or a console program.

Here, a little bit, you can quickly add components to the current project through the IntelliSense of this configuration paragraph, which is described in detail below.

"Commands": {

"Web": "Microsoft.AspNet.Hosting--server Microsoft.AspNet.Server.WebListener--server.urls http://localhost:5000"

},

The various commands built into the project are defined here, and a Web command line is defined here because the console server mode must be started by a command. You can start the console server mode by starting this command. In general, VS provides an intrinsic mechanism to start this command, but we want to understand that ASP . NET is actually running external commands through the core command Dnx.exe of the DNX environment, I feel that the current integration is not perfect, and that this command exists in a very strange place, and we are going to discuss this in more detail later in this section.

"Frameworks": {

"dnx451": {},

"Dnxcore50": {}

},

Define the version of the. Net framework that the project can run, by default supporting both the 4.5.1 and 5.0 core versions, removing any of them is possible, but note that 5.0 is not final release, and that some components may have compatibility issues with the 5.0 core version. It's time to make a choice.

"Publishexclude" and "exclude" mainly affect the content selection at the time of publishing and compiling. It has little effect on the main function.

Run for the first time

OK, let's run down to see what the effect is, choose Web mode (console server) to run, and a console program will pop up:

Then use the browser to access the http://localhost:5000 (this URL and Project.json in the parameters of the Web command configuration matches), you can see a "Hello World" page.

Why there is this page under the minimal template, we can see that in fact, the Configure function in startup has such a code:

        Public void Configure (Iapplicationbuilder app)        {            app. Run (async (context) =            {                await context. Response.writeasync ("Hello world! " );            });        }


There is a very small middleware implemented with lambda expressions, and of course this piece of code has no special meaning and can be removed.

Component Management

As I wrote in my previous article, the components of ASP. NET 5 must be managed by nuget, but in practice there are 2 ways to do it.

Through NuGet installation, this used VS2013 students should be very familiar with, of course, VS2015 interface slightly different, and more accurate, more complete function, we have a concrete look at the understanding.

And I would like to recommend the second way, in the Project.json file in the Dependencies section, directly using IntelliSense to add.

Load first middleware-static file Staticfile

Of course first we want to introduce staticfile components, using the direct modification of Project.json, can be quickly added:

"Dependencies": {

"Microsoft.AspNet.Server.IIS": "1.0.0-beta4",

"Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",

//static Files

"Microsoft.AspNet.StaticFiles": "1.0.0-beta4"

},

After modifying and saving, VS will automatically load this component into all the frameworks of the current project:

Here we load the middleware of the static file, we know that all middleware is loaded by the Configure function of startup, we replace the original code of this function with the following code.

      Public void Configure (Iapplicationbuilder app)        {            // load static file middleware             app. Usestaticfiles ();        }


According to the previous instructions we know that the static file middleware will default to the root directory is set to Wwwroot, below we create a index.html in this directory, put any content.

This is the time to start the whole project again (Web mode) with http://localhost:5000/index.html to access this page. (Conversely, if you haven't added an app.) Usestaticfiles (); This page is not accessible)

Below we are slightly expanded, if we want to set index.html as our default document, how we should do it, in fact very simple, in the Staticfiles component, also provides another middleware to implement this function, Let's go back to the Configure function and add the following code:

         Public void Configure (Iapplicationbuilder app)        {            // Set the default document app for static files            . Usedefaultfiles (new  defaultfilesoptions ()            {                newstring"  index.html"  }            });             // loading the static file middleware             app. Usestaticfiles ();        }


This time we need to introduce

Using Microsoft.AspNet.StaticFiles;

another userdefaultfiles must be in Userstaticfiles before running.

Then restart the project and you will see the index.html page with direct access to http://localhost:5000.

Of course Staticfiles Middleware is just a start, a really meaningful web application needs more content, and below I'll go through how to add MVC 6, WebApi 2, Entity Framework 7 in the ASP. NET 5 project, The implementation method of MS Identity 3. Please look forward to it.

Get started with ASP. 5 (1)-Build and develop an ASP. NET 5 Project

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.