本文介绍了为什么HomeController得到引用,我如何支持它(假设我应该)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一种Helleresque(Catch-22)情况;



使用这个Castle Windsor控制器工厂:


$ b如果没有修复, $ b

  public class WindsorControllerFactory:DefaultControllerFactory 
{
private readonly IKernel _kernel;

public WindsorControllerFactory(IKernel kernel)
{
_kernel = kernel;
//根据http://docs.castleproject.org/Windsor.Typed-Factory-Facility.ashx,可能需要这样:
// _ kernel.AddFacility< TypedFactoryFacility>(); //这断开了(已经注册的类型工厂设施),所以注释掉;请参阅http://stackoverflow.com/questions/20914635/how-do-i-connect-the-various-pieces-of-my-web-api-castle-windsor-di-code
//但是需要使用上面的注释TypedFactory。
}

protected override IController GetControllerInstance(RequestContext requestContext,Type controllerType)
{
if(controllerType == null)
{
throw新的HttpException(404,string.Format(找不到路径{0}的控制器),requestContext.HttpContext.Request.Path));
}
return(IController)_kernel.Resolve(controllerType);
}

public override void ReleaseController(IController controller)
{
_kernel.ReleaseComponent(controller);
}

}

/ p>

  _kernel.AddFacility< TypedFactoryFacility>(); 

...我得到这个运行时异常:typedFactoryFacility'容器



...如果我注释掉这行,我得到这个运行时异常:





...在此行上找不到



  return(IController)_kernel.Resolve(controllerType);为什么HomeController获得引用,我如何支持它(假设我应该)? > 

HomeController是一个默认类,如下所示:

  public class HomeController:控制器
{
public ActionResult Index()
{
ViewBag.Title =Home Page;

return View();
}
}

我应该摆脱HomeController吗?或... ...



UPDATE



我改变了HomeController:



> public class HomeController:Controller
{
public ActionResult Index()
{
ViewBag.Title = 主页;

return View();
}
}

...到此:

  public class HomeController:ApiController 
{
}

现在运行时异常来到这里:

  protected override IController GetControllerInstance (RequestContext requestContext,Type controllerType)
{
if(controllerType == null)//< - now controllerType is null
{
throw new HttpException(404,string。格式(找不到路径{0}的控制器。,requestContext.HttpContext.Request.Path));
}
return(IController)_kernel.Resolve(controllerType);
}

... is: System.Web.HttpException未处理用户代码
HResult = -2147467259
消息=无法找到路径'/'的控制器
源= HandheldServer
ErrorCode = -2147467259
WebEventCode = 0
StackTrace:
在HandheldServer.DIPlumbing.WindsorControllerFactory.GetControllerInstance(RequestContext requestContext,Type controllerType)in c:\HandheldServer\HandheldServer\DIPlumbing\WindsorControllerFactory.cs:line 36
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,String controllerName)
在System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController& controller,IControllerFactory& factory)
在系统。 Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)
在System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,AsyncCallback回调,对象状态)
在System.Web。 Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext上下文,AsyncCallback cb,对象extraData)
在System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

解决方案

检查您的路由是否未引用家庭控制器。

  routes.MapRoute(
name:Default,
url:{controller} / { action} / {id},
defaults:new {controller =Home,action =Index,id = UrlParameter.Optional});


This is sort of a Helleresque (Catch-22) situation; or perhaps more appropriately "unrepositoried if I do, and unrepositoried if I don't"

With this Castle Windsor Controller Factory:

public class WindsorControllerFactory : DefaultControllerFactory
{
    private readonly IKernel _kernel;

    public WindsorControllerFactory(IKernel kernel)
    {
        _kernel = kernel;
        //According to http://docs.castleproject.org/Windsor.Typed-Factory-Facility.ashx, might need this:
        //_kernel.AddFacility<TypedFactoryFacility>(); // This breaks ("Typed factory facility already registered"), so commenting it out; see http://stackoverflow.com/questions/20914635/how-do-i-connect-the-various-pieces-of-my-web-api-castle-windsor-di-code
        //but that requires uncommenting the "TypedFactory" using above.
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
        }
        return (IController)_kernel.Resolve(controllerType);
    }

    public override void ReleaseController(IController controller)
    {
        _kernel.ReleaseComponent(controller);
    }

}

If this line is live:

_kernel.AddFacility<TypedFactoryFacility>();

...I get this runtime exception: "TypedFactoryFacility' has already been registered with the container"

...If I comment that line out, I get this runtime exception:

"No component for supporting the service HandheldServer.Controllers.HomeController was found"

...on this line:

return (IController)_kernel.Resolve(controllerType);

Why is HomeController getting referenced, and how do I support it (assuming I should)?

HomeController is a default class that looks like this:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Title = "Home Page";

        return View();
    }
}

Should I get rid of HomeController? Or...???

UPDATE

I changed HomeController from this:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Title = "Home Page";

        return View();
    }
}

...to this:

public class HomeController : ApiController
{
}

And now the runtime exception I get here:

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
    if (controllerType == null) // <-- now controllerType is null
    {
        throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
    }
    return (IController)_kernel.Resolve(controllerType);
}

...is: "System.Web.HttpException was unhandled by user code HResult=-2147467259 Message=The controller for path '/' could not be found. Source=HandheldServer ErrorCode=-2147467259 WebEventCode=0 StackTrace: at HandheldServer.DIPlumbing.WindsorControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) in c:\HandheldServer\HandheldServer\DIPlumbing\WindsorControllerFactory.cs:line 36 at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()"

解决方案

Check that your routing is not referencing the home controller.

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

这篇关于为什么HomeController得到引用,我如何支持它(假设我应该)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:19