[转载]Getting Started with ASP.NET vNext and Visual Studio 14
说在转载之前的话:
ASP.NET框架之前不断做大,而vNext则是从头开始,对ASP.NET框架进行拆分并瘦身,面对不同的需求而更加灵活,各个拆分出来的模块更加轻量。
vNext的出现,对ASP.NET开发人员又是一次洗礼,ASP.NET开发人员是辛苦的,但也幸运的;俗话说,不进则退,vNext - 新的学习方向。
--------------------------------------------------------------------------------------------------------
www.asp.net/vnext/overview/aspnet-vnext/getting-started-with-aspnet-vnext-and-visual-studio
The next version of ASP.NET (“ASP.NET vNext”) has been redesigned from the ground up. The goal is to create a lean and composable .NET stack for building modern cloud-based apps.
You don't have to use Visual Studio to develop ASP.NET vNext applications. You can develop and run vNext on platforms that Visual Studio doesn't run on. But Visual Studio provides the best development experience, and this tutorial introduces you to that experience.
Note: This tutorial has been updated for CTP2.
Introduction to ASP.NET vNext
Here are some of the new features in ASP.NET vNext.
Rebuilt from the Ground Up
- MVC, Web API, and Web Pages are merged into one framework, called MVC 6. The new framework uses a common set of abstractions for routing, action selection, filters, model binding, and so on.
- Dependency injection is built into the framework. Use your preferred IoC container to register dependencies.
- vNext is host agnostic. You can host your app in IIS, or self-host in a custom process. (Web API 2 and SignalR 2 already support self-hosting; vNext brings this same capability to MVC.)
- vNext is open source and cross platform.
Leaner, Faster
- MVC 6 has no dependency on System.Web.dll. The result is a leaner framework, with faster startup time and lower memory consumption.
- vNext apps can use a cloud-optimized runtime and subset of the .NET Framework. This subset of the framework is about 11 megabytes in size compared to 200 megabytes for the full framework, and is composed of a collection of NuGet packages.
- Because the cloud-optimized framework is a collection of NuGet packages, your app can include only the packages you actually need. No unnecessary memory, disk space, loading time, etc.
- Microsoft can deliver updates to the framework on a faster cadence, because each part can be updated independently.
True Side-by-Side Deployment
The reduced footprint of the cloud-optimized runtime makes it practical to deploy the framework with your app.
- You can run apps side-by-side with different versions of the framework on the same server.
- Your apps are insulated from framework changes on the server.
- You can make framework updates for each app on its own schedule.
- No errors when you deploy to production resulting from a mismatch between the framework patch level on the development machine and the production server.
New Development Experience
vNext uses the Roslyn compiler to compile code dynamically.
- You can edit a code file, refresh the browser, and see the changes without rebuilding the project.
- Besides streamlining the development process, dynamic code compilation enables development scenarios that were not possible before, such as editing code on the server using Visual Studio Online ("Monaco").
- You can choose your own editors and tools.
ASP.NET vNext is being rewritten from the ground up, and while much of your code for vNext will look the same, vNext is not backwards compatible with existing ASP.NET applications. However, the current frameworks (Web Forms 5, MVC 5, Web API 2, Web Pages 3, SignalR 2, and Entity Framework 6) will continue to ship in Visual Studio, and will be fully supported in ASP.NET vNext.
Sections of this tutorial
- Intended audience
- Prerequisites
- Create an empty vNext project
- project.json
- Startup.cs
- Add a controller and view
- Create a vNext project and add a class library
- Deploy to Azure
- Visual Studio project and solution files
- Next steps
Intended audience
This tutorial assumes that you have worked with ASP.NET web projects in Visual Studio. It focuses on what's new in vNext and the new features in Visual Studio "14" CTP for working with vNext projects.
If you have worked with ASP.NET Web Forms or Web Pages but not MVC, it would be helpful to go through an Introduction to MVC first.
Prerequisites
This tutorial requires Visual Studio "14" CTP.
It is recommended to install the Visual Studio "14" CTP on a VM, VHD, or fresh computer. There are known side-by-side compatibility issues with Visual Studio 2013. Learn more about what’s in the Visual Studio “14” CTP.
For the optional section that deploys your vNext application to Azure, you'll need an Azure subscription to work with. If you don't already have an Azure account, but you do have an MSDN subscription, you can activate your MSDN subscription benefits. Otherwise, you can create a free trial account in just a couple of minutes. For details, see Windows Azure Free Trial.
Create an empty vNext project
You'll start by creating a vNext web project with the minimum required files and will take a look at those files. Then you'll add an MVC controller and view, and later you'll see how to add and reference a class library.
Start Visual Studio "14" CTP.
On the Start Page, click New Project, and then in the New Project dialog, select the C# / Web templates.
Select the ASP.NET vNext Empty Web Application template, name the project HelloWorld, and click OK.

In Solution Explorer you can see the two files that are required for a vNext web application: project.json and Startup.cs.

project.json
The project.json file contains a list of dependencies for the project and a list of build output configurations. It can also include a list of commands.
Dependencies
Dependencies are NuGet packages or other projects. For the empty vNext web project the only dependency is the Microsoft.AspNet.Server.IIS NuGet package, which enables the application to run in IIS.
{
"dependencies": {
"Microsoft.AspNet.Server.IIS" : "1.0.0-alpha2"
}
}
(Depending on when you do the tutorial, you might see a different version number.)
For NuGet package dependencies, Visual Studio asynchronously restores any missing packages when the project loads. You may have noticed that the HelloWorld project loaded instantly, but look at the Output window and you'll see that it took several seconds to restore Microsoft.AspNet.Server.IIS and a dozen or so packages that it depends on.

Build output configurations
The project.json file also specifies build output configuration options.
{
"configurations": {
"net45": { },
"k10": { }
}
}
These settings correspond to the Active Target Framework options in the Property Pages window in Visual Studio. To view this window, right-click the project in Solution Explorer and select Properties.

With the Active Target Framework still set to .NET 4.5.1, look at the References node in Solution Explorer.

Microsoft.AspNet.Server.IIS and other framework NuGet packages appear under the .NET Framework 4.5 node. If you expand the Microsoft.AspNet.Server.IIS node, you'll see that it combines dll references and NuGet package references and treats them as one unit. This hides dependencies and makes the references list more manageable.
Go back to the project properties page, change the Active Target Framework to .NET Core Framework 4.5, and then look at Solution Explorer again.
Visual Studio has changed the project references to reflect the fact that your project is now running under the cloud-optimized subset of the framework.

Commands
The project.json file can also contain commands that facilitate running the application from the command line. For example:
{
"commands": {
/* Change the port number when you are self hosting this application */
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
}
}
This tutorial does not show how to run vNext applications from the command line. For more information, see the project wiki on GitHub.
Startup.cs
By default, the vNext hosting environment expects to find a startup class named Startup. This class must contain a Configure method that takes an IBuilder parameter, and you configure the HTTP pipeline inside this Configure method. The empty project creates the class with nothing in the Configure method.
using System;
using Microsoft.AspNet.Builder; namespace HelloWorld
{
public class Startup
{
public void Configure(IBuilder app)
{
}
}
}
You'll add code to the Configure method when you add ASP.NET MVC functionality to the project in the next section of the tutorial.
Add a controller and view
To enable MVC in the HTTP pipeline you'll add a NuGet package and configure the Startup class.
In project.json, add a comma at the end of the Microsoft.AspNet.Server.IIS line, and then add the MVC package as shown. Type in the changes rather than copying and pasting, and don't save your changes yet.
{
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0-alpha2",
"Microsoft.AspNet.Mvc": "6.0-alpha2"
}
}Notice that you get IntelliSense for adding references.

The IntelliSense list is taken from the default NuGet package source and from other projects in folders that are siblings to the project folder.
The code in quotes after the colon specifies the version of the package to load. You can specify the exact version ("6.0-alpha2"), use an empty string to specify the latest available, or use wildcards to specify the latest available of a specified set ("1.0-alpha2-*").
Keep the Output window visible, and then save your changes.
You'll notice that Visual Studio watches for changes in the project.json file and automatically restores packages that are added to it. (Actual version numbers on your computer may be different from this screen shot.)

Visual Studio does not delete packages when you delete dependencies from project.json. But the packages folder is typically not included in source control, and the next time the project is retrieved from source control the deleted dependencies won't be restored.
To configure the
Startupclass, add the highlightedusingstatement and configuration code.using System;
using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
namespace HelloWorld
{
public class Startup
{
public void Configure(IBuilder app)
{
app.UseServices(services =>
{
services.AddMvc();
}); app.UseMvc(); }
}
}The
AddMvcmethod adds the MVC framework services to the dependency injection system. TheUseMvcmethod configures MVC default settings such as routes.
Create a Controllers folder, and in the folder create a new file named HomeController.cs, using the MVC Controller Class new item template.

- Create a Views folder.
- In the Views folder, create a subfolder named Home.
In the Home folder, add a view named Index.cshtml, using the MVC 5 View Page (Razor) new item template.

Replace the template code in the Index.cshtml page with the following code.
The current time is : @System.DateTime.Now.ToString()
Run the project.
The browser displays the date and time from the Index.cshtml page. By default the application is hosted by IIS Express.

Create a vNext project with a class library
In this section you'll create a new project by using the ASP.NET vNext Web Application template and see what it adds compared to the ASP.NET vNext Empty Web Application template. You'll see an example of vNext configuration settings, and then you'll add a class library project and set a reference to it in the web project.
From the File menu click New Project, and then in the New Project dialog, select the C# / Web templates.
Select the ASP.NET vNext Web Application template, name the project HelloWorld2, and click OK.

In addition to the project.json and the Startup.cs files, which are the same as what you've already seen, this project contains files needed for a simple MVC application, including CSS files, script files, and a Home controller and views. It also includes an Account controller and views, which enables users to log in, using ASP.NET Identity.

Look at the References node in Solution Explorer. Solution Explorer organizes the references so that it's not a long list to scroll through. For example, all of the MVC packages appear under the Microsoft.AspNet.Mvc node.

vNext configuration
The template creates a config.json file to store application settings such as database connection strings:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnetvnext-HelloWorld2-f0652fd2-febf-488d-955b-b4c590e536f1;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
In a vNext project, you can use a Web.config file for application settings, but you also have other options. For example, you can install NuGet packages with configuration providers for .ini files or .json files. The template uses Microsoft.Framework.ConfigurationModel.Json and creates a config.json. The Startup class includes this code that gets settings from config.json and also from environment variables:
var configuration = new Configuration();
configuration.AddJsonFile("config.json");
configuration.AddEnvironmentVariables();
The Configuration class also provides other methods for working with configuration data, such as a Get method that enables you retrieve a specified setting. The following code would retrieve the value of the connection string shown earlier in config.json.
var configuration = new Configuration();
configuration.AddJsonFile("config.json");
string connString = configuration.Get("Data:DefaultConnection:ConnectionString");
Add a class library project
Add to the solution an ASP.NET vNext Class Library project, leaving the default name ClassLibrary1. (Make sure you don't use the class library template under Windows Desktop; you have to use the vNext one under Web.)

In the HelloWorld2 web project, open the project.json file and add a line to set a reference to the class library project. Type in the new code instead of copying and pasting it in order to see that IntelliSense gives you ClassLibrary1 as an option you can select.
{
"dependencies": {
"EntityFramework.SqlServer": "7.0.0-alpha2",
"Microsoft.AspNet.Mvc": "6.0.0-alpha2",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-alpha2",
"Microsoft.AspNet.Identity.Authentication": "3.0.0-alpha2",
"Microsoft.AspNet.Security.Cookies": "1.0.0-alpha2",
"Microsoft.AspNet.Server.IIS": "1.0.0-alpha2",
"Microsoft.AspNet.Server.WebListener": "1.0.0-alpha2",
"Microsoft.AspNet.StaticFiles": "1.0.0-alpha2",
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-alpha2",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0-alpha2",
"Classlibrary1": ""
},When you save the file, the References node in Solution Explorer shows the class library project and identifies it as a project in the properties window (other references such as Microsoft.AspNet.Server.IIS show up as NuGet package references).

In the ClassLibrary1 project, open Class1.cs and add the highlighted code to create a method that returns a "hello world" value.
public class Class1
{
public Class1()
{ }
public static string GetMessage()
{
return "Hello World from ClassLibrary1!";
}}In the HelloWorld2 project, open Controllers\HomeController.cs and call the new class library method from the About action method.
public IActionResult About()
{
ViewBag.Message = Classlibrary1.Class1.GetMessage(); return View();
}Press CTRL+F5 to run the project (don't run in debug mode).
You see the home page in the browser.

Go to the About page to see your hello world message.

With the browser still open, open Class1.cs and change the message text to "New message from ClassLibrary1!".
Save the file. Don't rebuild the project.
Refresh the browser.
You see the result of your changes in the browser. The code file was recompiled without your having to rebuild the project.
(If the changes don't appear in the browser, make sure you ran the project by pressing CTRL+F5, not just F5. Dynamic recompile works by restarting the process. That doesn't happen when you're debugging because Visual Studio would have to detach from the process.)

If you look at the bin folder, you'll see there are no project dlls there -- your source code is dynamically compiled to memory.
Deploy to Azure
In this optional section, you deploy the project to the cloud and run it there.
Right-click the HelloWorld2 project and click Publish.
In the Publish Web wizard, click Azure Web Sites.

Sign in to Azure if you aren't already signed in.
Click New to create a new web site in Azure.

Enter a unique site name, choose a region near you, and then click Create.

As an alternative, you could also choose to create a database in order to register and log on users in the cloud using ASP.NET Identity. The process for doing that is the same as in the current ASP.NET release.
On the Connection tab, click Publish.

Visual Studio publishes the project to Azure and then opens a browser to the home page.

Visual Studio project and solution files
ASP.NET vNext is designed to work on many platforms, including those on which Visual Studio doesn't run. If you create a project in one of those other environments and want to work on it in Visual Studio, all you have to do is open the project.json file and Visual Studio automatically creates the other files that it needs: a solution file (.sln) and a project file (.kproj). In this section of the tutorial you'll see how this works.
Open your HelloWorld2 solution folder in File Explorer
Close the solution in Visual Studio.
Set File Explorer to show hidden files and file extensions (View tab, select Hidden items and File name extensions).
Delete the .suo and .sln files, and the .sln.ide and packages folders.
In the ClassLibrary1 and HelloWorld2 project folders, delete the .kproj and .kproj.user files and the bin and obj folders.
Now all you have left is project source code and project.json files. This is all you need to develop, compile, and run a vNext application if you want to do that without using Visual Studio.
In Visual Studio, from the File menu, select Open > Project/Solution. and open the project.json file in the HelloWorld2 project folder.
Based on the contents of the folders and the dependencies in the project.json file, Visual Studio figures out the entire solution structure and automatically re-creates the solution, as you can see in Solution Explorer.

Click the Save All Files icon to save the new .sln and .kproj files.
Open the HelloWorld2 solution folder in File Explorer, and you'll see that the packages folder has also been restored.

Open the ClassLibrary1 project folder, and you'll see that the .kproj files are back.

Leave File Explorer opened to the ClassLibrary1 project.
You can also make changes outside of Visual Studio after opening a project in Visual Studio, and those changes are automatically picked up.
Open the Class1.cs file in a text editor such as Notepad, change both occurrences of "Class1" to "Class2", and then save the file in the same folder as Class2.cs.
Go back to Visual Studio, and you see that the new file is automatically added to the project.

Build and Run vNext Apps from the Command Line
You can build and run vNext applications from within Visual Studio or from the command-line. The project.json file contains commands that facilitate running the application from the command line. For example:
{
"commands": {
/* Change the port number when you are self hosting this application */
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
}
}
In this section, you will use the web command provided by the Visual Studio template to run the application from the command-line and host it in its own process. To set up your environment to run commands you will need to install the K Version Manager (KVM) tool, which is used to retrieve different versions of the runtime and allow you to switch between them. You then run commands using the K script.
- Run the following command from a command-line prompt to install the K Version Manager.
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.ps1'))" - Run
kvm listto see the set of runtimes installed on your machine. Currently none are active for use on the command-line. - Run
kvm aliasto see the set of available aliases for runtime versions. Aliases make it easier to specify the specific version of the runtime you want to use. Notice that Visual Studio has already set up a default alias. - Run
kvm use defaultto use the version of the runtime specified by that alias. Run kvm list again to see that the desktop (not the cloud-optimized) version of the runtime is now active. - Switch directories to the directory of the project.json file containing the web command for the app.
- Run
k webto start the application. - Once the console output indicates that the app is started, browse to the address specified in your web command (http://localhost:5000/) to see the running application.
Next Steps
In this tutorial you've seen a quick introduction to developing ASP.NET vNext applications using Visual Studio. For more information about ASP.NET vNext and for vNext sample applications, see the following resources:
- http://asp.net/vnext
- Introducing ASP.NET vNext on Scott Hanselman's blog.
- ASP.NET vNext on David Fowler's blog.
- The Next Generation of .NET – ASP.NET vNext on the .NET Framework blog.
- ASP.NET vNext: the future of .NET on the Server on the .NET Web Development and Tools blog.
- aspnet on GitHub.
Feedback is welcome. You can provide feedback in GitHub, in comments on this page, or in the ASP.NET VNext forum. If you ask a question in StackOverflow, use the asp.net-vnext tag. You can make suggestions for new features on the UserVoice site.
[转载]Getting Started with ASP.NET vNext and Visual Studio 14的更多相关文章
- 在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移
在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移 最近发布的ASP.NET MVC 5 及Visual ...
- ASP.NET 4 and Visual Studio 2010
https://msdn.microsoft.com/en-us/library/ee532866.aspx The topics in this section provide informatio ...
- 开发 ASP.NET vNext 初步总结(使用Visual Studio 14 CTP1)
新特性: vNext又称MVC 6.0,不再需要依赖System.Web,占用的内存大大减少(从前无论是多么简单的一个请求,System.Web本身就要占用31KB内存). 可以self-host模式 ...
- Visual Studio 14 初试,vNext
下了几天的VS 2014 .终于安装上了,花了好几天时间, VS 2014 下载地址, http://www.visualstudio.com/en-us/downloads/visual-stud ...
- ASP.NET MVC3在Visual Studio 2010中的变化
在VS2010中新建一个MVC3项目可以看出与以往的MVC2发生了很明显的变化 1.ASP.NET MVC3必要的运行环境为.NET 4.0 (想在3.5用MVC3,没门!) 2.默认MVC3模板项目 ...
- Visual Studio 2015 开发 ASP.NET 5 有何变化?
本篇博文目录: ASP.NET 5 模版 ASP.NET 5 目录结构 前端管理工具 无编译开发 Microsoft Git Provider 智能感知和错误信息 Smart Unit Testing ...
- Visual Studio 2015 开发 ASP.NET 5 有何变化?(转)
出处:http://www.cnblogs.com/xishuai/p/visual-studio-2015-preview-asp-net-5-change.html 本篇博文目录: ASP.NET ...
- 分享我对 ASP.NET vNext 的一些感受,也许多年回过头看 So Easy!
写在前面 阅读目录: Visual Studio "14" CTP 关于 ASP.NET vNext ASP.NET vNext 实践 后记 ASP.NET vNext 发布已经过 ...
- 【转载】保哥 釐清 CLR、.NET、C#、Visual Studio、ASP.NET 各版本之間的關係
我常常不仅仅逛 博客园,还会去找国外,特别是台湾的技术部落格,发现好的文章,我便会收录,今天我转载或者全文复制,在Google 博客园,一位叫保哥, 釐清 CLR..NET.C#.Visual Stu ...
随机推荐
- HTTPS链式编程——AFNetworking 3.0
1. HTTPS 证书认证(导入相关证书) #pragma mark - https认证 - (AFSecurityPolicy*)customSecurityPolicy { // 先导入证书 NS ...
- Angular与PHP之间的不同的请求方式(post/get)导致的传参问题
angularJS的$http发送POST请求,PHP无法接受数据的问题 使用jQuery进行ajax请求 $.ajax({ type: 'POST', url:'url.php', data: da ...
- web安全测试系统
最近写了个简单的web安全实践系统部署到了docker中 下载方式:docker pull ju5ton1y/websecurity github Dockerfile下载地址:https://git ...
- ACM数论之旅16---母函数(又名生成函数)(痛并快乐着(╭ ̄3 ̄)╭)
(前排出售零食瓜子) 前言: 母函数是个很难的东西,难在数学 而ACM中所用的母函数只是母函数的基础 应该说除了不好理解外,其他都是非常简单的 母函数即生成函数,是组合数学中尤其是计数方面的一个重要理 ...
- 【Java并发编程】之十二:线程间通信中notifyAll造成的早期通知问题
如果线程在等待时接到通知,但线程等待的条件还不满足,此时,线程接到的就是早期通知,如果条件满足的时间很短,但很快又改变了,而变得不再满足,这时也将发生早期通知.这种现象听起来很奇怪,下面通过一个示例程 ...
- Day22-Django之缓存
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...
- Jackson 使用
// 序列化出来的 JSON, 不包含值为 NULL 类型字段. mapper.setSerializationInclusion(Include.NON_NULL); Jackson provide ...
- All flavors must now belong to a named flavor dimension
FAQ: All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/ ...
- 解题:NOI 2014 动物园
题面 其实好像并不难,因为猫老师(应该是猫老师吧,还是LX大佬?)有一句话让我印象深刻:“包的(border)的包的还是包的”=.= 统计个数不就是统计长度么,然后根据上面那句话,当$nxt$长度大于 ...
- EndNote文献悬挂缩进的设置方法及设置参考文献序号后面空格长度
一.EndNote文献悬挂缩进的设置方法 写论文时使用EndNote来插入和管理参考文献是一种非常方便的方法,但有时不同的杂志要求插入的文献要求第二行缩进格式,或者不缩进. 1.在EndNote中Ed ...