c# - 如何将 WebApi 2 与 Spring.Net 集成

标签 c# asp.net-web-api asp.net-mvc-5 spring.net

我尝试将 WebApi 2Spring.Net 集成,但没有成功。这是我尝试过的:

public class MvcApplication : SpringMvcApplication
{
    protected void Application_Start()
    {
        XmlConfigurator.Configure();

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

创建 SpringWebApiDependencyResolver 实例时,我收到此异常:

创建上下文“spring.root”时出错:请求在此上下文中不可用

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.DependencyResolver = new SpringWebApiDependencyResolver(ContextRegistry.GetContext());

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

我使用 Spring.Net 2.0.1

最佳答案

现在可以工作了,不需要以下行:

config.DependencyResolver = new SpringWebApiDependencyResolver(ContextRegistry.GetContext());

这是我基于Spring.Net的实现example :

public class MvcApplication : SpringMvcApplication
{
    protected void Application_Start()
    {
        // More config...
        GlobalConfiguration.Configure(WebApiConfig.Register);
        // More config...
    }

    protected override System.Web.Http.Dependencies.IDependencyResolver BuildWebApiDependencyResolver()
    {
        var resolver = base.BuildWebApiDependencyResolver();

        var springResolver = resolver as SpringWebApiDependencyResolver;

        if (springResolver != null)
        {
            var resource = new AssemblyResource(
                "assembly://assemblyName/namespace/ChildControllers.xml");
            springResolver.AddChildApplicationContextConfigurationResource(resource);
        }

        return resolver;
    }
}

我更喜欢保留通用的 WebApi 配置:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

在 IoC 容器中注册 Controller 与 MVC 集成相同:

<object type="namespace.PrefixController, assemblyName" singleton="false" >
    <property name="Dependency1" ref="Dependency1" />
</object>

关于c# - 如何将 WebApi 2 与 Spring.Net 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930847/

相关文章:

javascript - 使用 Angular Js 从 API 获取 Json 数据

jquery - OnActionExecuting 事件未返回 jsonResult

c# - MVC Owin Cookie 身份验证 - 覆盖 ReturnUrl 生成

c# - 迭代数组并将强类型添加到列表中

c# - EF 迁移应该去哪里,我的类库项目还是我的 ASP.NET 项目?

c# - 将多行插入数据库,其中每一行引用 C# LINQ 中的另一行

entity-framework - "Validation failed for one or more entities. See ' EntityValidationErrors 属性的解决方案以获取更多详细信息。”

c# - 如何从 PropertyGridView 中删除标准属性

.net - .net 核心 web api 中的 IAuthorizationFilter 不支持 AllowAnyonmous 属性

c# - ASP.Net Web API 自定义模型与 x-www-form-urlencoded 发布数据绑定(bind) - 似乎没有任何效果