Discussion: Startup Code changes in MVC #3083
Description
This is the place for all feedback and discussion related to aspnet/Announcements#62
For beta7 we've changed MVC's startup code APIs to be more consistent with EntityFramework and Identity.
Part 1:
Both the AddMvc(...)
and AddMvcCore(...)
extension methods now return a builder API, which can be chained to additional setup methods:
services.AddMvc(options =>
{
options.Filters.Add(...);
})
.AddXmlDataContractSerializerFormatters()
.AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder);
Or used as local variable:
var mvc = services.AddMvc(options =>
{
options.Filters.Add(...);
})
mvc.AddXmlDataContractSerializerFormatters()
mvc.AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder);
The purpose of this change (beyond consistency) is to make the intellisense experience more targeted. Now MVC methods that require you to register MVC can't be called without doing so. Also MVC's various features are more discoverable if you're looking at intellisense for the builder.
Part 2:
We removed additional extension methods that we had off of IServiceCollection
or MvcOptions
and made them into extensions methods targeting one of the MVC builders (IMvcCoreBuilder
or IMvcBuilder
).
Old:
services.AddMvc();
services.ConfigureMvc(options =>
{
options.AddXmlDataContractSerializerFormatters();
})
New:
services.AddMvc().AddXmlDataContractSerializerFormatters();
Part 3:
We removed variations on ConfigureMvcXyz(...)
and replaced them with extension methods targeting one of the builders.
All of our AddXyzFeature(...)
methods that have an associated options type, we provide an overload that takes an Action<T>
for configuring options.
Where there's no obvious AddXyzFeature(...)
we provide an AddXyzOptions(...)
method instead.
Old:
services.ConfigureMvcJson(...);
New:
services.AddMvc().AddJsonOptions(...);