<tfoot id='vPe4e'></tfoot>

    • <bdo id='vPe4e'></bdo><ul id='vPe4e'></ul>
    <legend id='vPe4e'><style id='vPe4e'><dir id='vPe4e'><q id='vPe4e'></q></dir></style></legend>
    <i id='vPe4e'><tr id='vPe4e'><dt id='vPe4e'><q id='vPe4e'><span id='vPe4e'><b id='vPe4e'><form id='vPe4e'><ins id='vPe4e'></ins><ul id='vPe4e'></ul><sub id='vPe4e'></sub></form><legend id='vPe4e'></legend><bdo id='vPe4e'><pre id='vPe4e'><center id='vPe4e'></center></pre></bdo></b><th id='vPe4e'></th></span></q></dt></tr></i><div id='vPe4e'><tfoot id='vPe4e'></tfoot><dl id='vPe4e'><fieldset id='vPe4e'></fieldset></dl></div>

    <small id='vPe4e'></small><noframes id='vPe4e'>

    1. 我应该使用 AddMvc 还是 AddMvcCore 进行 ASP.NET Core MVC 开发?

      Should I use AddMvc or AddMvcCore for ASP.NET Core MVC development?(我应该使用 AddMvc 还是 AddMvcCore 进行 ASP.NET Core MVC 开发?)
        <tbody id='3OM1W'></tbody>

        <tfoot id='3OM1W'></tfoot>
        <i id='3OM1W'><tr id='3OM1W'><dt id='3OM1W'><q id='3OM1W'><span id='3OM1W'><b id='3OM1W'><form id='3OM1W'><ins id='3OM1W'></ins><ul id='3OM1W'></ul><sub id='3OM1W'></sub></form><legend id='3OM1W'></legend><bdo id='3OM1W'><pre id='3OM1W'><center id='3OM1W'></center></pre></bdo></b><th id='3OM1W'></th></span></q></dt></tr></i><div id='3OM1W'><tfoot id='3OM1W'></tfoot><dl id='3OM1W'><fieldset id='3OM1W'></fieldset></dl></div>

        • <bdo id='3OM1W'></bdo><ul id='3OM1W'></ul>
            • <small id='3OM1W'></small><noframes id='3OM1W'>

              • <legend id='3OM1W'><style id='3OM1W'><dir id='3OM1W'><q id='3OM1W'></q></dir></style></legend>

              • 本文介绍了我应该使用 AddMvc 还是 AddMvcCore 进行 ASP.NET Core MVC 开发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在从一本书中学习ASP.NET Core MVC,有问题的代码片段如下:

                I am learning ASP.NET Core MVC from a book, the code snippet in question is as follows:

                // CHAPTER 4 - ESSENTIAL C# FEATURES
                namespace LanguageFeatures {
                
                    public class Startup {
                
                        public void ConfigureServices(IServiceCollection services) {
                            services.AddMvc();
                        }
                
                        // etc.
                

                因为这本书是关于 ASP.NET Core MVC 而不是 ASP.NET MVC,所以我想我必须使用 AddMvcCore() 而不是 AddMvc() 如下:

                Because the book is about ASP.NET Core MVC rather than ASP.NET MVC, I think I have to use AddMvcCore() rather than AddMvc() as follows:

                public void ConfigureServices(IServiceCollection services)
                {
                    services.AddMvcCore(); // as opposed to:
                    //services.AddMvc();
                }
                

                我在这里做的正确吗?

                推荐答案

                看看MvcServiceCollectionExtensions.cs 类 rel="nofollow noreferrer">ASP.NET Core GitHub repo:

                public static IMvcBuilder AddMvc(this IServiceCollection services)
                {
                    if (services == null)
                    {
                        throw new ArgumentNullException(nameof(services));
                    }
                
                    var builder = services.AddMvcCore();
                
                    builder.AddApiExplorer();
                    builder.AddAuthorization();
                
                    AddDefaultFrameworkParts(builder.PartManager);
                
                    // Order added affects options setup order
                
                    // Default framework order
                    builder.AddFormatterMappings();
                    builder.AddViews();
                    builder.AddRazorViewEngine();
                    builder.AddRazorPages();
                    builder.AddCacheTagHelper();
                
                    // +1 order
                    builder.AddDataAnnotations(); // +1 order
                
                    builder.AddCors();
                
                    return new MvcBuilder(builder.Services, builder.PartManager);
                }
                

                AddMvcCore()AddMvc() 都返回一个 IMvcBuilder,可用于进一步配置 MVC 服务.

                AddMvcCore() and AddMvc() both return an IMvcBuilder that can be used to further configure the MVC services.

                AddMvcCore(),顾名思义,只添加MVC管道的核心组件,需要你自己添加任何其他中间件(项目需要).

                AddMvcCore(), as the name implies, only adds core components of the MVC pipeline, requiring you to add any other middleware (needed for your project) by yourself.

                AddMvc() 内部调用 AddMvcCore() 并添加其他中间件,例如 Razor 视图引擎、Razor 页面、CORS 等.

                AddMvc() internally calls AddMvcCore() and adds other middleware such as the Razor view engine, Razor pages, CORS, etc.

                现在,我会按照您的教程建议并坚持 AddMvc().

                For now, I would follow what your tutorial suggests and stick to AddMvc().

                从 ASP.NET Core 3.0 开始,还有其他方法可以细粒度控制 MVC 管道的哪些部分可用于您的应用程序,例如:

                As of ASP.NET Core 3.0, there are additional methods that give fine-grained control over what portions of the MVC pipeline are available to your application, e.g.:

                • services.AddControllers()
                • services.AddControllersWithViews()

                参考 这篇文章 和 MSDN 了解有关它们的作用以及何时使用它们的更多信息.

                Refer to this article and MSDN for more information about what they do and when to use them.

                这篇关于我应该使用 AddMvc 还是 AddMvcCore 进行 ASP.NET Core MVC 开发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                Accessing attribute info from DTE(从 DTE 访问属性信息)
                c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                C# Attributes and their uses(C# 属性及其用途)
                C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                <legend id='4XeRO'><style id='4XeRO'><dir id='4XeRO'><q id='4XeRO'></q></dir></style></legend>
                  • <bdo id='4XeRO'></bdo><ul id='4XeRO'></ul>
                    • <small id='4XeRO'></small><noframes id='4XeRO'>

                        <tbody id='4XeRO'></tbody>
                      <i id='4XeRO'><tr id='4XeRO'><dt id='4XeRO'><q id='4XeRO'><span id='4XeRO'><b id='4XeRO'><form id='4XeRO'><ins id='4XeRO'></ins><ul id='4XeRO'></ul><sub id='4XeRO'></sub></form><legend id='4XeRO'></legend><bdo id='4XeRO'><pre id='4XeRO'><center id='4XeRO'></center></pre></bdo></b><th id='4XeRO'></th></span></q></dt></tr></i><div id='4XeRO'><tfoot id='4XeRO'></tfoot><dl id='4XeRO'><fieldset id='4XeRO'></fieldset></dl></div>
                    • <tfoot id='4XeRO'></tfoot>