Preliminary Summary of ASP. NET vNext Development (using Visual Studio 2014 CTP1)

Source: Internet
Author: User

New features:

VNext, also known as MVC 6.0, no longer needs to rely on System. Web, and greatly reduces the memory usage (no matter how simple a request was previously, System. Web itself will occupy 31 KB of memory ).
It can run in self-host mode, and websites no longer need to rely on IIS. This allows you to quickly demonstrate the development progress to customers and better support real cross-platform (Linux, Mac, and Unix ).
Web APIs, Web Pages, and MVC are fully integrated, collectively referred to as MVC 6.0. In the past, JSON can be output either by using the return JSON that comes with MVC or by using Web APIs. In this way, the isolated and governed scenes have a negative impact on the entire community (JSON is only one example, all other functions are the same.) Now, the Web Api is fully integrated with vNext, And the repetitive wheel is finally terminated here.
The Poco Controller is supported, and the code is cleaner.
Cloud optimization technology, that is to say, in the development stage, the system itself does not really load a certain component, but queries the appropriate version of the component at runtime, the benefit of this is that on Azure cloud hosts, Mono hosts, and Windows hosts, you can rely on a component Dll that best fits the current environment.
Built-in dependency injection framework
Use the Roslyn compiler to generate more efficient code
 
And 1 ~ 5. x differences:

1: Project. json replaces the previous Web. config:

  • The dependencies of the project package are defined in the dependencies node.
  • The deployments node saves the project settings.
  • The commands node stores the self-host configuration parameters, such as the locally bound port number.
  • Custom nodes, such as customizing a Data node, define a DefaultConnectionString in its scope to record database connection strings.

2: Startup. cs replaces Global. asax
 
3: no special Web. config is required in the Views folder.

When you add a reference (such as Entity Framework), the network needs to be loaded in real time. So if you encounter an error "cannot find a class library", don't worry. It will take a while. However, you may not be able to wait for a long time on a auspicious day, because we are in the tianchao, you know.
 
========================================================== ==========
 
The basic concepts of MVC remain unchanged, and the previous RouteTable, Controller, View, Action, Razor, Model
 

The following tutorial involves loading packages and the version numbers of each package, such as "0.1-alpha-build-0403". The version numbers may not apply when you see this article, the simplest solution is to create a ready-made ASP. NET vNext Web Application (note that it is not a blank project), it loads the currently available frequently used packages and compares them with the corresponding package version number.


If you need to output static files, such as .html#.jpg, You need to load special processing procedures, which are saved in Microsoft. AspNet. StaticFiles.

Next we will start to build a vNext site:

Create an empty ASP. NET vNext Project

 

The obtained directory structure is much simpler than before:

 

First configure project. json and add it to the dependencies node:

"Microsoft. aspNet. staticFiles ":" 0.1-alpha-build-0403 "// After static file output is saved, note the status changes in the lower left corner: network-based during recovery, loading is complete only after it is restored. If you find that the two steps do not exist, try the method you know. The nuget server is in the United States. I can only tell you this.

 
Configure Startup. cs:

Using System; using Microsoft. aspNet. builder; using Microsoft. aspNet. staticFiles; using Microsoft. framework. dependencyInjection; using Microsoft. aspNet. routing; namespace vNextTry {public class Startup {public void Configure (IBuilder app) {app. useStaticFiles (); // supports static file output app. useServices (services => {services. addMvc (); // load MVC module}); app. useMvc (routes => // load route configuration {routes. mapRoute (name: "Default", t Emplate: "{controller}/{action}/{id ?} ", Defaults: new {controller =" Home ", action =" Index "});});}}}

Add a traditional Controller:

using Microsoft.AspNet.Mvc;   public class HomeController : Controller {     public ActionResult Index()     {         return View();     } }

Add a Poco Controller:

Public class JsonData // does not need to inherit from any class {public dynamic Index () {// in this way, output JSON: {"id": 1, "name": "I'm haha"} return new {id = 1, name = "I'm haha "};}}

Add a View:
Note that Index is placed in Views/{controller name}/{here is Index}. cshtml, and the master page will not be used. The methods such as Model and RenderBody are the same as before:

<!DOCTYPE html> 

Everything is done. Now you can run the test.

 

Run the application in self-host Mode
Self-host is the biggest feature of MVC6. Our applications no longer need to configure huge IIS, making it no longer difficult to quickly demonstrate applications on the client.
Configure self-host by setting project. json (note 5001, that is, the bound port ):

Dependencies node addition: "Microsoft. AspNet. Server. WebListener": "0.1-alpha-build-0520"

"Commands ":{
"Web": "Microsoft. AspNet. Hosting server = Microsoft. AspNet. Server. WebListener server. urls = http: // localhost: 5001"
}

Then save and release the package to the output directory (Be sure to save it before nuget resumes loading)

Run web. cmd in the output directory to enable the Service.

Run applications in IIS Mode:
Currently, it can only be published to the local IIS that is installed in the development environment. However, according to its running logic, some parameters can be configured in principle to run the program on a remote server, let's not talk about it.
If it is a 64-bit system, may encounter the problem of cannot find the KRE-svr50-x64.0.1-alpha-build-0446, Currently Microsoft only provides 32-bit version, we can solve this problem only by setting the application pool to 32-Bit mode.

Use Web APIs:
It becomes simpler:
1: configure the route in Startup. cs:
App. UseMvc (routes =>
{
Routes. MapRoute ("ApiRoute", "{controller}/{id ?} ");
});
 
2: In controller:

using Microsoft.AspNet.Mvc;  public class ValuesController : Controller {     // GET /values     public string Get()     {         return "Values";     }      // GET /values/1     public string Get(int id)     {         return "Value " + id.ToString();     }       // POST /values     public ActionResult Post()     {         return new HttpStatusCodeResult(201);     } }

  

In this way, no package or DLL needs to be loaded, no private Config needs to be configured, And the API is fully integrated into MVC6 to access localhost: Port Number/api Class Name

 

Conclusion:

If all pages are "inaccessible" during Visual Studio debugging, check whether the current service port of IIS Express is consistent in the IE window displayed, turn Visual Studio off (and check the system process to make sure that there are no other processes) and re-open Visual Studio.

Because the package loading involves loading from nuget.org at runtime, some friends who put the App in domestic host vendors should be prepared.

To run vNext on * nix in the near future, you should pay attention to the following points:

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.