gpt4 book ai didi

model-view-controller - SportsStore : MVC programming issue [CaSTLe WindsorControllerFactory]

转载 作者:行者123 更新时间:2023-12-04 00:15:14 25 4
gpt4 key购买 nike

所以我一直在关注 Steven Sanderson 的名为 Pro ASP.NET MVC Framework 的书,但我遇到了一个异常:

No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error:

Line 16: HttpContext.Current.RewritePath(Request.ApplicationPath, false);
Line 17: IHttpHandler httpHandler = new MvcHttpHandler();
Line 18: httpHandler.ProcessRequest(HttpContext.Current);
Line 19: HttpContext.Current.RewritePath(originalPath, false);
Line 20: }


Source File: C:\Users\Stephen\Documents\Visual Studio 2008\Projects\SportsStore\WebUI\Default.aspx.cs Line: 18

这是我的 WindsorControllerFactory 代码:
public class WindsorControllerFactory : DefaultControllerFactory
{
WindsorContainer container;

// The constructor
// 1. Sets up a new IoC container
// 2. Registers all components specified in web.config
// 3. Registers all controller types as components
public WindsorControllerFactory()
{
// Instantiate a container, taking configuration from web.config
container = new WindsorContainer(
new XmlInterpreter(new ConfigResource("castle"))
);

// Also register all the controller types as transient
var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;

foreach (Type t in controllerTypes)
container.AddComponentWithLifestyle(t.FullName, t, Castle.Core.LifestyleType.Transient);
}

// Constructs the controller instance needed to service each request
protected override IController GetControllerInstance(Type controllerType)
{
return (IController)container.Resolve(controllerType);
}
}

我的 Global.asax.cs 代码:
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory());
}

和 web.config 值:
<configSections>
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
Castle.Windsor"/>
</configSections>
<castle>
<properties>
<myConnStr>Server=.\SQLEXPRESS;Database=SportsStore;Trusted_Connection=yes;</myConnStr>
</properties>
<components>
<component id="ProdsRepository"
service="DomainModel.Abstract.IProductsRepository, DomainModel"
type="DomainModel.Concrete.SqlProductsRepository, DomainModel">
<parameters>
<connectionString>#{myConnStr}</connectionString>
</parameters>
</component>
</components>
</castle>

谢谢大家!
-史蒂夫

最佳答案

namespace WebUI
{
public class WindsorControllerFactory:DefaultControllerFactory
{
WindsorContainer container;

public WindsorControllerFactory()
{
container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));

container.Register(AllTypes
.FromThisAssembly()
.BasedOn<IController>()
.If(Component.IsInSameNamespaceAs<Controllers.ProductsController>())
.If(t => t.Name.EndsWith("Controller"))
.Configure(c=>c.LifeStyle.Transient.Named(c.Implementation.Name)));
}

protected override IController GetControllerInstance(RequestContext ctx, Type controllerType)
{
if (controllerType == null) { return null; }
return (IController)container.Resolve(controllerType);
}
}
}

关于model-view-controller - SportsStore : MVC programming issue [CaSTLe WindsorControllerFactory],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1285605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com