MVC 6.0.0-beta7 is released

Yes, You heard it right. .Net team has released beta7 of MVC 6 before 2 days and now it is available in Nuget as well you can directly download it.

It contains some new features as well as some bug fixes for previous versions.

What are some features from beta-7?
1. Client side validation of float and double types:

Scenario:
If you have used previous beta for MVC 6 you might know that float and double uses type=”text”.
For example for float it would be as below:

<input type="text" value="1.5" />

Any Impact?:
It disables the browser specific validation of number fields.

Fixed?
Yes, It has been added as a feature for beta 7

2. Returning Ok() from action method:

Scenario:
Suppose you are using MVC5 and there is an Action method in which you are expecting an object.
For that you might do as below:

[HttpGet("{id}")]
public IActionResult Detail(int id)
{
 var ret = xxx.GetById(id);
 if (ret == null) return HttpNotFound($"id {id} does not exist");
 return new HttpStatusCodeResult(ret);
}

Any Impact?:
A missing helper in controller to return an object

Fixed?
Yes, It has been added as a feature for beta 7 and now you can directly return Ok(ret);

3. IMvcBuilder overloads for localization and XML formatters:

Scenario:
If you have used previous beta for MVC 6 you might know that there’s now a configuration experience where you can start with a minimal MVC pipeline and add features to get a customized framework.

public void ConfigureServices(IServiceCollection services)
{
 services
 .AddMvcCore()
 .AddAuthorization()
 .AddJsonFormatters(j => j.Formatting = Formatting.Indented);
}

Here AddMvcCore returns a separate interface IMvcBuilder.

Any Impact?:
Now suppose you want to add EntityFramework then you would write something as below:

public void ConfigureServices(IServiceCollection services)
{
 services
 .AddMvcCore()
 .AddAuthorization()
 .AddJsonFormatters(j => j.Formatting = Formatting.Indented)
 .AddEntityFramework(); // <-- This won't work because we're dealing with `IMvcBuilder` here
}

Fixed?
Both the AddMvc(…) and AddMvcCore(…) extension methods now return a builder API, which can be chained to additional setup methods.
So now above scenario works!

4. Razor attribute behavior to not special case boolean values:

Scenario:

Today if a user writes:

@{
  var isActive = true;
}

<input type="button" onclick="doSomething(@isActive);" />

Any Impact?:
It generates:

<input type="button" onclick="doSomething(onclick);" />.

If isActive is false it removes the content entirely.

Fixed?
In beta 7 Razor boolean and null attribute special case handled correctly.
So in above case If isActive is false it would give:

<input type="button" >

5. Returning ViewComponents from controller actions:

First of all What is view components?

Partial views in MVC has one limitation, which is that they doesn’t have a controller for them. Not having controller makes it hard to have more complex logic associated with the view. View components changes that. Each view component consist of a view and a backing class, it’s not a controller but almost.

Scenario:
In order to render a ViewComponent from a controller, you have to create a view that simply has:

MyViewComponent.cshtml

So if you want to return this ViewComponent you have to return view instead of this Component as below:

public IActionResult MyViewComponent()
{
 return View("MyViewComponent");
}

Fixed?
Yes, in beta 7 they have added ViewComponentResult. So you can directly return ViewComponent.

6. Support for model binding dictionaries

Scenario:

Consider the model:

public class Foo
{
 public Dictionary<int, int> SomeDisctionary{ get; set; }
}

Any Impact?:
The name generated by Html.NameFor(v => v.Values[1]) is Values[1] however this expression does not get model bound.

Fixed?
Yes, in beta 7 they have added support for model binding dictionaries.

7. Added URL resolving TagHelper to replace Razor ~/ functionality

Scenario:

Classic Razor would resolve the value of any HTML attribute that started with ~/ as an application relative URL. This led to some invalid scenarios where users would provide attribute values such as ~/folder/file.txt and get inaccurate URL resolutions. In addition, this feature couldn’t be easily disabled.

Fixed:

Yes, .Net team decided to create the UrlResolutionTagHelper to take over ~/ resolution.
But for that you need to disable Url resolution as shown below:

@removeTagHelper "Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNet.Mvc.Razor"

Additionally there are some bugs fixed in this beta version.

You can have 6.0.0-beta7 here at Nuget:

Stay tuned for more updates.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: