As I already Written blog for Asp.Net 5 and MVC 6 difference on my previous blog.
Here I am going to little more explore about Asp.Net 5 and talking about new feature included in Asp.Net 5.
Listing few features,
Lets see how to inject TestClass into the view directly and defined a TestClass property by which it can be accessed.
Here I am going to little more explore about Asp.Net 5 and talking about new feature included in Asp.Net 5.
Listing few features,
- Asp.Net on Cross-Platform
- No More Web Forms
- Tag Helpers
- View Components
- Many Client side framework supported. (Gulp, NPM and Bower Support)
- Common Controllers for MVC and Web API
- AngularJS
- Inbuilt Dependency Injection Framework
- xUnit.net
- Asp.net 5 application run on cross-platforms like windows, OSX and Linux.
- Developers and designers can start building apps with Asp.Net 5 because of no visual studio required.
- So they can use their favorite IDE or development environments editors such as Sublime Text, Atom, Emacs and Brackets.
- Now, Its time to move forward for those who love Asp.Net Web Forms.
- Because of Web Forms apps can not take advantage from new features of Asp.Net 5.
- If you still want to continue buld Web forms apps in Visual Studio 2015 then need to targeting the .NET 4.6 framework.
- Biggest change on Asp.Net MVC application when you create your views.
- Because of Tag Helpers are a better alternative of using traditional MVC helpers.
@model FirstProject.Models.Categories @using (Html.BeginForm()) {@Html.LabelFor(m => p.Name, "Name:") @Html.TextBoxFor(m => p.Name)}
- In the above example of view, the Html.BeginForm(), Html.LabelFor() and Html.TextBoxFor() helpers are used to create the form.
- HTML Designer would not be familiar with above helpers.
@model FirstProject.Models.Categories @addtaghelper "Microsoft.AspNet.Mvc.TagHelpers"
- Time to compare both the view and notice that using Tag Helpers, created form contains look like HTML elements.
- It meant form contains an Input element instead of Html.TextBoxFor() helper. So it would be fine HTML designer fine with this page.
- Tag Helpers introduce one special asp-for attributes on html tag. These attributes are used to extend the elements with server side Asp.Net MVC functionality.
- Time to say Goodbye subcontrollers and adapt View Components.
- If you remember, in previous versions of ASP.NET MVC, the Html.Action() helper is typically used to invoke a sub-controller. A sub-controller may display stuff like tag clouds, dynamic links, side bars or whatever.
- ASP.NET MVC 6 introduced the new View Component to replace widgets that use Html.Action().
using Microsoft.AspNet.Mvc; using System; namespace Partials.Components { public class TextPrint : ViewComponent { public IViewComponentResult Invoke() { return View("_TextPrint", "Hello This is for test!"); } } }Here how look like the _TextPrint partial.
@model stringNow, let see how we can usre the TextPrint View Compenent in MVC view.@Model
@Component.Invoke("TextPrint")
- View Components are very similar to subcontrollers.
- However, subcontrollers were always a little odd.
- They were pretending to be controller actions but they were not really controller actions.
- Front-end development gets a lot of love in Asp.Net 5 through its support for Gulp.
- GruntJS is a task runner that enables you to build front-end resources such as JavaScript and CSS files. For example, you can use GruntJS to concatenate and minify your JavaScript files whenever you perform a build in Visual Studio.
- In order to support GruntJS, Microsoft needed to support two new package managers (beyond NuGet). First, because GruntJS plugins are distributed as NPM packages, Microsoft added support for NPM packages.
- Second, because many client-side resources – such as Twitter Bootstrap, jQuery, Polymer, and AngularJS – are distributed through Bower, Microsoft added support for Bower.
- This means that you can run GruntJS using plugins from NPM and client resources from Bower.
- In previous versions of ASP.NET MVC, MVC controllers were different than Web API controllers. An MVC controller used the System.Web.MVC.Controller base class and a Web API controller used the System.Web.Http.ApiController base class.
- In MVC 6, there is one and only one Controller class that is the base class for both MVC and Web API controllers. There is only the Microsoft.AspNet.Mvc.Controller class.
- MVC 6 controllers return an IActionResult. When used as an MVC controller, the IActionResult might be a view. When used as a Web API controller, the IActionResult might be data (such as a list of products). The same controller might have actions that return both views and data.
- In MVC 6, both MVC controllers and Web API controllers use the same routes. You can use either convention-based routes or attribute routes and they apply to all controllers in a project.
- Now a days AngularJs is most popular client-side frameworks for building SPAs (Single Page Applications).
- So for fortunatly Visual Stdio 2015 includes templates for creating AngularJS modules, controllers, directives and factories.
- The support in ASP.NET 5 for Gulp makes ASP.NET an excellent server-side framework for building client-side AngularJS apps.
- You can combine and minify all of your AngularJS files automatically whenever you perform a build.
- You can interact with an MVC 6 controller from an AngularJS $resource using REST.
- ASP.NET 5 has built-in support for Dependency Injection and the Service Locator pattern. This means that you no longer need to rely on third-party Dependency Injection frameworks such as Ninject or AutoFac.
- Previously need do many things for Dependency Injection and Resolver but in Asp.Net 5 has built-in support.
- In Asp.Net 5 you can use the [Activate] attribute to inject services via properties.
- You can use [Activate] not just on controllers but also on filters and view components.
public class HomeController : Controller { [Activate] public TestClass TestClass { get; set; } }Asp.Net 5 also supports Dependency Injection into Razorviews via the @inject keyword.
Lets see how to inject TestClass into the view directly and defined a TestClass property by which it can be accessed.
@using WebApplication23 @inject TestClass Testclass9. xUnit.net@ViewBag.Message
- In Asp.Net we can say Goodbye Visual Studio Unit Testing Framework and welcome to xUnit.net.
- Previous versions of Asp.Net MVC, the default testing framework was the Visual Studio Unit Testing Framework. This framework uses the [TestClass] and [TestMethod] attributes to describe a unit test.
[TestClass] public class ProductTests { [TestMethod] public void TestAddProduct() { //Write your code } }
- In Asp.Net 5 replace the [TestClass] and [TestMethod] to [Fact] attribute.
- It means xUnit.net framework uses the [Fact] attribute.
- xUnit just use [Fact] attribute while previous version unit test framework used [TestMethod] and [TestClass].
public class ProductTests { [Fact] public void TestAddProduct() { //Write your code } }
0 comments:
Post a Comment