从另一个类库基地控制器没有在网页API工作 [英] Base controller from another class library doesn't working in web api

查看:258
本文介绍了从另一个类库基地控制器没有在网页API工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Web API项目,我有一个 MarketController ,我需要扩展API控制器,​​所以我做到了。

我创建了一个 BaseController 类和 ApiController 继承是这样的:

 公共类BaseController:ApiController {}

到目前为止好,它的正常工作:

 公共类MarketController:BaseController
{
    公共MarketController():基地()
    {
    }    //获取API /<控制器与GT;
    公共IEnumerable的<串GT;得到()
    {
        返回新的字符串[] {值1,值2};
    }
}

不过,我想做到这一点在另一个名为类库 BLL ,让我感动的 BaseController 类的 BLL 类库,我引用它在Web API项目。

当我这样做,该API停止工作。

的反应是:

  {
    消息:没有HTTP资源发现,请求URI someurl / API /市场匹配,
    MessageDetail:没有类型被发现,命名为'市场'的控制器相匹配。
}


解决方案

默认情况下MVC会在MVC应用程序相同的程序集中的所有控制器。默认的控制器工厂基于字符串创建控制器实例ControllerName +控制器就像MarketController是在市场来自URL的市场/ actionname 的。它会寻找MarketController在同一组装为MVC应用程序。

要放在控制器单独的程序,你必须创建自己的控制器工厂,或者您必须指定程序集名称是应用程序的开始。

一旦你创建自己的自定义的ControllerFactory,你将下面的行添加到的Application_Start在Global.asax中告诉框架在哪里找到它:

  ControllerBuilder.Current.SetControllerFactory(新MyControllerFactory());

或者简单的情况下,像你这样的,你可以这样做:

  routes.MapRoute(
    默认,
    {控制器} / {行动} / {ID}
    新{控制器=家,行动=索引,ID =},
    新的[] {BLLAssembly.Controllers}
);

下面BLLAssembly.Controllers是在BLL装配您BaseController命名空间。

有使用自定义程序集解析器,即多了一个高级的方式 IAssembliesResolver
下面的文章介绍如何使用Web API实现还,

<一个href=\"http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/\" rel=\"nofollow\">http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/

I have two Web API projects and I have a MarketController and I need to extend the Api controller so I did it.

I created a BaseController class and inherit from ApiController like this:

public class BaseController:ApiController { }

so far so good, it's working fine:

public class MarketController : BaseController
{
    public MarketController() : base()
    {
    }

    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

But I wanted to do it in another class library called BLL, so I moved the BaseController class to the BLL class library and I referenced it in the Web API project.

When I did this, the api stopped working.

The response is :

{
    "Message": "No HTTP resource was found that matches the request URI someurl/api/market.",
    "MessageDetail": "No type was found that matches the controller named 'market'."
}

解决方案

By default MVC looks for all controllers in same assembly of mvc application. The default controller factory creates controller instance based on string 'ControllerName+Controller' like MarketController where market comes from url market/actionname.It will look for MarketController in the same assembly as mvc application.

To put controller in separate assembly you will have to create your own controller factory or you will have to specify assembly name is app start.

Once you've created your own custom ControllerFactory, you add the following line to Application_Start in global.asax to tell the framework where to find it:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

Or for simple cases like yours you can do this :

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" },
    new[] { "BLLAssembly.Controllers" }
);

Here BLLAssembly.Controllers is namespace for your BaseController in BLL assembly.

There is one more advanced way using custom assembly resolver ,i.e IAssembliesResolver The below article tells how to do this with Web Api also,

http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/

这篇关于从另一个类库基地控制器没有在网页API工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆