在现有的ASP.NET MVC 5项目中使用WebAPI 2.2

约旦

我在现有MVC 5项目中使用带有属性路由的WebAPI 2.2。我打算将整个网站迁移到WebAPI,但这需要一些时间。我一切正常,但我担心自己可能做错了什么。

该SO帖子似乎建议我应该在Global.asax.cs文件中调用GlobalConfiguration.Configure(WebApiConfig.Register)

如果我只是删除通常在中提供的HttpConfiguration参数WebApiConfig.Register(),并GlobalConfiguration.Configure(x => x.MapHttpAttributeRoutes())WebApiConfig.Register()方法中简单地调用-WebAPI端点将以所需的结果进行响应。

所以这就是我的最终结果:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}


class WebApiConfig
{
    public static void Register()
    {
        GlobalConfiguration.Configure(x => x.MapHttpAttributeRoutes());
    }
}

这种方法有什么问题吗?

约旦

因此,事实证明,只有两种方法可以解决将WebAPI 2.2添加到现有项目时出现的配置问题。我正在做两个修复程序,当我阅读代码时,这些修复程序对我来说很清楚。

以下:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}


class WebApiConfig
{
    public static void Register()
    {
        GlobalConfiguration.Configure(x => x.MapHttpAttributeRoutes());
    }
}

实际上与执行以下操作相同:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        //WebApiConfig.Register();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {
        configuration.MapHttpAttributeRoutes();
    }
}

看来我只是在经历精神失常:)

很明显,GlobalConfiguration.Configure(x => x.MapHttpAttributeRoutes())它基本上与做相同的事情GlobalConfiguration.Configure(WebApiConfig.Register)

从逻辑上讲,这些应该产生相同的结果。这是System.Web.Http命名空间中GlobalConfiguration的Microsoft代码:

/// <summary>
/// Provides a global <see cref="T:System.Web.Http.HttpConfiguration"/> for ASP.NET applications.
/// </summary>
public static class GlobalConfiguration
{
    private static Lazy<HttpConfiguration> _configuration = CreateConfiguration();

    ///... code excluded for brevity

    /// <summary>
    /// Gets the global <see cref="T:System.Web.Http.HttpConfiguration"/>.
    /// </summary>
    public static HttpConfiguration Configuration
    {
        get { return _configuration.Value; }
    }

    /// <summary>
    /// Performs configuration for <see cref="GlobalConfiguration.Configuration"/> and ensures that it is
    /// initialized.
    /// </summary>
    /// <param name="configurationCallback">The callback that will perform the configuration.</param>
    public static void Configure(Action<HttpConfiguration> configurationCallback)
    {
        if (configurationCallback == null)
        {
            throw new ArgumentNullException("configurationCallback");
        }

        configurationCallback.Invoke(Configuration);
        Configuration.EnsureInitialized();
    }

    private static Lazy<HttpConfiguration> CreateConfiguration()
    {
        return new Lazy<HttpConfiguration>(() =>
        {
            HttpConfiguration config = new HttpConfiguration(new HostedHttpRouteCollection(RouteTable.Routes));
            ServicesContainer services = config.Services;
            Contract.Assert(services != null);
            services.Replace(typeof(IAssembliesResolver), new WebHostAssembliesResolver());
            services.Replace(typeof(IHttpControllerTypeResolver), new WebHostHttpControllerTypeResolver());
            services.Replace(typeof(IHostBufferPolicySelector), new WebHostBufferPolicySelector());
            services.Replace(typeof(IExceptionHandler),
                new WebHostExceptionHandler(services.GetExceptionHandler()));
            return config;
        });
    }

    ///... code excluded for brevity
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

如何将Web API添加到现有的ASP.NET MVC(5)Web应用程序项目中?

来自分类Dev

在同一项目中使用MVC5和Web API 2

来自分类Dev

将Funscript添加到现有的ASP.NET MVC项目

来自分类Dev

在Xcode 5项目中使用Core Plot 2

来自分类Dev

在WebApi2项目中使用CacheOutput属性时,依赖项的解析失败

来自分类Dev

我的MVC 5 Identity 2项目中没有ApplicationRoleManager类

来自分类Dev

将jTable(jQuery)集成到使用默认布局的ASP.NET MVC 5项目中

来自分类Dev

在ASP NET MVC 6项目中使用System.Net.Mail

来自分类Dev

如何将Websharper与现有的ASP.NET MVC项目集成?

来自分类Dev

我应该在ASP.NET-MVC 5项目中的哪个位置存储WebAPI控制器?

来自分类Dev

在多个项目中使用Asp.net MVC身份

来自分类Dev

为现有的Azure App Service创建新的ASP.NET 5项目

来自分类Dev

使用Angular 2设置ASP.NET MVC 4或5项目

来自分类Dev

转换现有的angular 2项目以使用angular CLI

来自分类Dev

将客户端Blazor添加到现有的ASP.NET MVC5应用程序

来自分类Dev

在Qt 5项目中使用Nite 2

来自分类Dev

在Xcode 5项目中使用Core Plot 2

来自分类Dev

在WebApi2项目中使用CacheOutput属性时,依赖项的解析失败

来自分类Dev

在asp.net mvc4项目中使用draggable()jQuery函数?

来自分类Dev

在ASP.NET MVC项目中使用WebControl

来自分类Dev

如何将Websharper与现有的ASP.NET MVC项目集成?

来自分类Dev

使用WCF更新现有的asp.net项目

来自分类Dev

将自托管的Asp.NET MVC 5添加到现有的winforms / console项目中

来自分类Dev

如何在同一个ASP.Net MVC项目中使用Unity MVC和Unity WebAPI

来自分类Dev

使用Angular 2设置ASP.NET MVC 4或5项目

来自分类Dev

在ASP.NET MVC项目中托管多个Angular 2应用程序

来自分类Dev

为什么在 ASP.NET 项目中使用 Angular 2?

来自分类Dev

在 asp.net core 2 项目中使用路由提供角度服务

来自分类Dev

在 ASP.net Core MVC 2 中使用 ASP.net MVC 5 脚手架

Related 相关文章

  1. 1

    如何将Web API添加到现有的ASP.NET MVC(5)Web应用程序项目中?

  2. 2

    在同一项目中使用MVC5和Web API 2

  3. 3

    将Funscript添加到现有的ASP.NET MVC项目

  4. 4

    在Xcode 5项目中使用Core Plot 2

  5. 5

    在WebApi2项目中使用CacheOutput属性时,依赖项的解析失败

  6. 6

    我的MVC 5 Identity 2项目中没有ApplicationRoleManager类

  7. 7

    将jTable(jQuery)集成到使用默认布局的ASP.NET MVC 5项目中

  8. 8

    在ASP NET MVC 6项目中使用System.Net.Mail

  9. 9

    如何将Websharper与现有的ASP.NET MVC项目集成?

  10. 10

    我应该在ASP.NET-MVC 5项目中的哪个位置存储WebAPI控制器?

  11. 11

    在多个项目中使用Asp.net MVC身份

  12. 12

    为现有的Azure App Service创建新的ASP.NET 5项目

  13. 13

    使用Angular 2设置ASP.NET MVC 4或5项目

  14. 14

    转换现有的angular 2项目以使用angular CLI

  15. 15

    将客户端Blazor添加到现有的ASP.NET MVC5应用程序

  16. 16

    在Qt 5项目中使用Nite 2

  17. 17

    在Xcode 5项目中使用Core Plot 2

  18. 18

    在WebApi2项目中使用CacheOutput属性时,依赖项的解析失败

  19. 19

    在asp.net mvc4项目中使用draggable()jQuery函数?

  20. 20

    在ASP.NET MVC项目中使用WebControl

  21. 21

    如何将Websharper与现有的ASP.NET MVC项目集成?

  22. 22

    使用WCF更新现有的asp.net项目

  23. 23

    将自托管的Asp.NET MVC 5添加到现有的winforms / console项目中

  24. 24

    如何在同一个ASP.Net MVC项目中使用Unity MVC和Unity WebAPI

  25. 25

    使用Angular 2设置ASP.NET MVC 4或5项目

  26. 26

    在ASP.NET MVC项目中托管多个Angular 2应用程序

  27. 27

    为什么在 ASP.NET 项目中使用 Angular 2?

  28. 28

    在 asp.net core 2 项目中使用路由提供角度服务

  29. 29

    在 ASP.net Core MVC 2 中使用 ASP.net MVC 5 脚手架

热门标签

归档