Base solution for your next web application
Open Closed

Getting ILocalizationManager injected into AbpWebViewPage<T> #63


User avatar
0
langman66 created

I'm using ABP framework in a MVC and SPA application. The front-end application is MVC to benefit from SEO.

My application supports multiple languages (thanks to Abp awesome framework).

What I need to do is build a dynamic header menu for different language supported on the _Layout.cshtml base razor layout page.

In my _Layout.cshtml, I split my top menu into a Partial.

@Html.Partial("_TopBar")

Every page uses the _Layout.cshtml for their base template (Index.cshtml, Contact.cshtml, etc.).

Below is my _TopBar.cshtml partial page.

Question:

In my base MVC page public abstract class CaptureWebViewPageBase<TModel> : AbpWebViewPage<TModel>

I'm trying to use Property injection to get an public ILocalizationManager LocalizationManager { get; set; } but this object is not being set.

If I use constructor injection, then I get compile errors telling me "CaptureWebViewPageBase<TModel> doesn't contain parameterless constructor."

How can I inject the a ILocalizationManager into this page or am I doing this totally the wrong way?

Thanks so much for the guidance.

<div class="pre-header">

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
        
        &lt;div class=&quot;col-md-6 col-sm-6 additional-shop-info&quot;&gt;
            &lt;ul class=&quot;list-unstyled list-inline&quot;&gt;
                &lt;li&gt;
                    &lt;i class=&quot;fa fa-envelope-o&quot;&gt;&lt;/i&gt;
                    &lt;span&gt;
                        &lt;a href=&quot;mailto:[email protected]&quot;&gt;[email protected]&lt;/a&gt;
                    &lt;/span&gt;
                &lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;
        

        
        &lt;div class=&quot;col-md-6 col-sm-6 additional-nav&quot;&gt;
            &lt;ul class=&quot;list-unstyled list-inline pull-right&quot;&gt;

                @using Microsoft.AspNet.Identity
                @if (Request.IsAuthenticated)
                {
                    &lt;li&gt;@Html.ActionLink(L("MyAccount"), "Dashboard", "App")&lt;/li&gt;
                    &lt;li&gt;@Html.ActionLink(L("Logout"), "Logout", "Account", routeValues: null, htmlAttributes: new { id = "logoutForm", target = "_self" })&lt;/li&gt;
                }
                else
                {
                    &lt;li class=&quot;dropdown dropdown-language&quot;&gt;
                        &lt;a class=&quot;dropdown-toggle&quot; dropdown-menu-hover data-toggle=&quot;dropdown&quot; data-close-others=&quot;true&quot;&gt;
                            &lt;i&gt;&lt;/i&gt;
                            &lt;span class=&quot;langname&quot;&gt;@L(@CurrentLanguage)  &lt;/span&gt;
                            &lt;i class=&quot;fa fa-angle-down&quot;&gt;&lt;/i&gt;
                        &lt;/a&gt;
                                                    
                        &lt;ul class=&quot;dropdown-menu dropdown-menu-default&quot;&gt;
                                @foreach (var language in Languages)
                                {
                                    &lt;li class=&quot;active&quot;&gt;
                                        &lt;a href=&quot;/AbpLocalization/[email protected]&quot;&gt;
                                            &lt;i class=&quot;@language.Icon&quot;&gt;&lt;/i&gt; @L(language.DisplayName)
                                        &lt;/a&gt;
                                    &lt;/li&gt;
                                }                                
                        &lt;/ul&gt;

                    &lt;/li&gt;

                    &lt;li&gt;
                        @Html.ActionLink(@L("Login"), "Login", "Account")
                    &lt;/li&gt;
                }

            &lt;/ul&gt;
        &lt;/div&gt;
        
    &lt;/div&gt;
&lt;/div&gt;

</div>


2 Answer(s)
  • User Avatar
    0
    langman66 created

    I ended up putting this in my constructor protected CaptureWebViewPageBase(){

            if (LocalizationManager == null)
            {
                LocalizationManager = IocManager.Instance.Resolve&lt;ILocalizationManager&gt;();                
            }
    
                Languages = LocalizationManager.GetAllLanguages();
                CurrentLanguage = LocalizationManager.CurrentLanguage;
    

    }

    Still not sure if this is the best approach, but it works for now :-)

  • User Avatar
    0
    hikalkan created
    Support Team

    This is the only option since we can not inject objects to views.