- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了能够在我的 ASP.NET Core 应用程序中添加 Controller ,我可以添加
services.AddControllersWithViews()
services.AddMvc()
ConfigureServices
方法在
Startup
类(class)。
services.AddMvc()
是较旧的方式,但仍然可用。
services.AddMvc()
将来会不会有问题?
最佳答案
源代码不言自明
MvcServiceCollectionExtensions.AddMvc()
/// <summary>
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
public static IMvcBuilder AddMvc(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddControllersWithViews();
return services.AddRazorPages();
}
/// <summary>
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
var builder = services.AddMvc();
builder.Services.Configure(setupAction);
return builder;
}
/// <summary>
/// Adds services for controllers to the specified <see cref="IServiceCollection"/>. This method will not
/// register services used for pages.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
/// <remarks>
/// <para>
/// This method configures the MVC services for the commonly used features with controllers with views. This
/// combines the effects of <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/>,
/// <see cref="MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(IMvcCoreBuilder)"/>,
/// <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/>,
/// <see cref="MvcCorsMvcCoreBuilderExtensions.AddCors(IMvcCoreBuilder)"/>,
/// <see cref="MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(IMvcCoreBuilder)"/>,
/// <see cref="MvcCoreMvcCoreBuilderExtensions.AddFormatterMappings(IMvcCoreBuilder)"/>,
/// <see cref="TagHelperServicesExtensions.AddCacheTagHelper(IMvcCoreBuilder)"/>,
/// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/>,
/// and <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
/// </para>
/// <para>
/// To add services for pages call <see cref="AddRazorPages(IServiceCollection)"/>.
/// </para>
/// </remarks>
public static IMvcBuilder AddControllersWithViews(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
var builder = AddControllersWithViewsCore(services);
return new MvcBuilder(builder.Services, builder.PartManager);
}
/// <summary>
/// Adds services for controllers to the specified <see cref="IServiceCollection"/>. This method will not
/// register services used for pages.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="configure">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
/// <remarks>
/// <para>
/// This method configures the MVC services for the commonly used features with controllers with views. This
/// combines the effects of <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/>,
/// <see cref="MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(IMvcCoreBuilder)"/>,
/// <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/>,
/// <see cref="MvcCorsMvcCoreBuilderExtensions.AddCors(IMvcCoreBuilder)"/>,
/// <see cref="MvcDataAnnotationsMvcCoreBuilderExtensions.AddDataAnnotations(IMvcCoreBuilder)"/>,
/// <see cref="MvcCoreMvcCoreBuilderExtensions.AddFormatterMappings(IMvcCoreBuilder)"/>,
/// <see cref="TagHelperServicesExtensions.AddCacheTagHelper(IMvcCoreBuilder)"/>,
/// <see cref="MvcViewFeaturesMvcCoreBuilderExtensions.AddViews(IMvcCoreBuilder)"/>,
/// and <see cref="MvcRazorMvcCoreBuilderExtensions.AddRazorViewEngine(IMvcCoreBuilder)"/>.
/// </para>
/// <para>
/// To add services for pages call <see cref="AddRazorPages(IServiceCollection)"/>.
/// </para>
/// </remarks>
public static IMvcBuilder AddControllersWithViews(this IServiceCollection services, Action<MvcOptions> configure)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
// This method excludes all of the view-related services by default.
var builder = AddControllersWithViewsCore(services);
if (configure != null)
{
builder.AddMvcOptions(configure);
}
return new MvcBuilder(builder.Services, builder.PartManager);
}
AddMvc
基本上是结束对
AddControllersWithViews
的调用, 加上调用
AddRazorPages
.
If I keep using services.AddMvc() would it be a problem in the future?
关于c# - services.AddControllersWithViews() 与 services.AddMvc(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62251347/
当我尝试在 visual studio 2015 中构建项目时,我在 ConfigureServices 方法中遇到了问题。 最佳答案 检查您尝试使用的 asp.net-mvc 版本。看起来您正在混合
我试图找出为什么在所有文档中我看到 services.AddMvc或 services.AddMvcCore在 Startup.cs 但是在由 VS 创建的 ASP.NET Core MVC 3.1
为了能够在我的 ASP.NET Core 应用程序中添加 Controller ,我可以添加 services.AddControllersWithViews() 要么 services.AddMvc
自 2018 年 5 月 30 日起,我在 Startup.cs 中的 ASP.NET Core 代码 public IServiceProvider ConfigureServices(IServi
我正在学习 ASP.NET Core,我看到注册 MVC 服务如下所示: public void ConfigureServices(IServiceCollection services) {
当我使用 Swagger 为我的 API 提供动力时,我遵循了 those guids 之一而且我总是像这样将 MVC 注入(inject)放在 Swagger 注入(inject)之前。 servi
我正在将 ASP.NET Core 2.2 Web 应用程序迁移到 3.0,并且有关于 .AddMvc() 的澄清问题。如果这很重要,我的应用程序使用 Razor 页面和 View 。 所以目前,我在
我使用以下命令创建了 .net core 2.0 mvc 应用程序:“dotnet new mvc”,它运行没有任何问题,但在 vscode 中我有两个错误: 'IServiceCollection'
我创建了一个全新的 asp.net 核心应用程序。我已经添加了 mvc Nuget 包,但出现以下错误。 微软.AspNetCore.Mvc.Core 1.1.3 'IServiceCollectio
我正在为 Asp.Net Core 创建一个 Nuget 包。我想让配置变得简单。因此,我决定提供一种流畅的方法,将我的 Nuget 添加到 Asp.Net Core 中 ConfigureServi
我正在学习本教程:http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6设置一个 web api,但我还没
我正在看书学习ASP.NET Core MVC,问题代码片段如下: // CHAPTER 4 - ESSENTIAL C# FEATURES namespace LanguageFeatures {
当使用 .NET core Web API 的脚手架时,它包括: services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Vers
我不太喜欢使用预捆绑的 AddMvc(),而是更喜欢使用 AddMvcCore()。 话虽如此,我想知道如何将新的(从 2.0 开始)AddRazorPages() 与 AddMvcCore() 一起
为什么在 mvc6 应用程序的 Configuration 方法中只添加 app.UseMvc() 是不够的?为什么还需要在 ConfigureServices 方法中添加 services.AddM
我是一名优秀的程序员,十分优秀!