我有一個小應用程序(現在)使用翻譯。 我的翻譯服務被稱爲翻譯(ITranslator)。簡單的噴油器 - 自定義WebViewPage
我的問題是我不想在每個控制器(它返回一個視圖)中檢索這個類,所以我可以在我的視圖中使用它。
我有一個ListService類似的問題(我存儲我所有的SelectListItem
列表)。
CustomWebViewPage
public abstract class CustomViewPage<TModel> : WebViewPage<TModel>
{
readonly IScriptFactory scriptFactory;
readonly IMembership membership;
readonly IListService listService;
readonly ITranslator translator;
IHtmlString path;
public IMembership Membership { get { return this.membership; } }
public override IPrincipal User { get { return this.membership.User; } }
public IListService Lists { get { return this.listService; } }
public ITranslator Translator { get { return this.translator; } }
public CustomViewPage(IScriptFactory scriptFactory, IMembership membership,
IListService listService, ITranslator translator)
{
this.scriptFactory = scriptFactory;
this.membership = membership;
this.listService = listService;
this.translator = translator;
}
public IHtmlString OwnScriptsPath
{
get { return path ?? (path = scriptFactory.Create(this.Html).ToHtmlString()); }
}
private string languageHtml =
"<meta name=\"language\" content=\"" + membership.CurrentLanguage + "\">";
public IHtmlString MetaLanguage { get { return MvcHtmlString.Create(languageHtml); } }
}
與用法:
<div>
@Lists.GetNationalities(Model.NationalityId)
</div>
但是,在運行我有以下錯誤應用程序時:
'[NAMESPACE].CustomViewPage' does not contain a constructor that takes 0 arguments
但是,如果我創建第二個構造函數Simple-Injector會拋出一個錯誤sinc它不支持多個構造函數。
我該如何實現我想要的,或者不可能?
編輯
完整的堆棧跟蹤
c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vs\520dfaa2\f5e4578c\App_Web_index.cshtml.a8d08dba.jtlk7fxz.0.cs(40): error CS1729: 'SportsHub.SDK.Infrastructure.CustomViewPage' does not contain a constructor that takes 0 arguments
at System.Web.Compilation.AssemblyBuilder.Compile()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) at System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetCompiledType(String virtualPath)
at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.GetCompiledType(String virtualPath)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
編輯2
由於@StriplingWarrior和@Steven建議,我用財產注入,但它不似乎正在努力WebViewPage
我在一項服務上進行了測試,並且Property注入成功,但在此課程中,屬性爲null
。 PS:我使用get; set方法公開屬性,
新的ViewPage:
public abstract class CustomViewPage<TModel> : WebViewPage<TModel>
{
[Inject]
public IScriptFactory scriptFactory { get; set; }
[Inject]
public IMembership membership { get; set; }
[Inject]
public IListService listService { get; set; }
[Inject]
public ITranslator translator { get; set; }
IHtmlString scriptsPath;
public IHtmlString OwnScriptsPath()
{
if (scriptsPath == null)
{
scriptsPath = scriptFactory.Create(this.Html).ToHtmlString();
}
return scriptsPath;
}
/// <summary>
/// Renders the current user language with a meta tag
/// </summary>
public IHtmlString MetaLanguage()
{
return MvcHtmlString.Create("<meta name=\"language\" content=\"" + membership.CurrentLanguage + "\">");
}
對於InjectAttribute
我已經按照以下documents。
這可能是相關的:http://stackoverflow.com/questions/7656195/can-you-inject-dependencies-into-a-constructor-of-a-custom-webviewpage-using-an – StriplingWarrior
@StriplingWarrior I'我已經看到了這些具有注入屬性的例子但簡單注入器不提供它們 –
請向我們提供您正在獲取的異常的完整堆棧跟蹤。 – Steven