【解決方法】.NET6 で html.renderaction を置き換える方法

プログラミングQA

[ad_1]

こんにちは。ASP .NET MVC アプリを .NET Framework 4.8 から .NET 6 に移行していますが、System.Web.Mvc.Html 名前空間の Html.RenderAction メソッドが機能しなくなり、エラーが発生するという問題が発生しました。私のコード:

かみそり
@{
		   if (Model.IsCrawler)
		   {
		 	Html.RenderAction("GetLanguageSelector", "languageSelectorAsync", new { brand = Model.Layout.BrandShortName, cultureInfo = Model.CurrentPage.CurrentLanguage });
}
	}

このメソッドは、LanguageSelectorAsyncController にアクセスしています。

C#
public class LanguageSelectorAsyncController : Microsoft.AspNetCore.Mvc.Controller
   {
       private readonly IContentLoader contentLoader;
       private readonly IContentLanguageAccessor contentLanguageAccessor;

       public LanguageSelectorAsyncController(
           IContentLoader contentLoader, IContentLanguageAccessor contentLanguageAccessor)
       {
           this.contentLoader = contentLoader;
           this.contentLanguageAccessor = contentLanguageAccessor;
       }

       public Microsoft.AspNetCore.Mvc.ActionResult GetLanguageSelector(string brand, CultureInfo cultureInfo)
       {
           contentLanguageAccessor.Language = cultureInfo;
           HomePage homePage = ContentReference.StartPage.Get<HomePage>();
           if (homePage?.SiteSettingsLink != null)
           {
               SiteSettings settingsPage = contentLoader.Get<SiteSettings>(homePage.SiteSettingsLink, cultureInfo) ?? contentLoader.Get<SiteSettings>(homePage.SiteSettingsLink);

               if (settingsPage != null)
               {
                   if (brand == "rockstar")
                   {
                       return PartialView("Content/BrandSelectorBlock", settingsPage.RockstarLanguageSelector);
                   }
                   else if (brand == "green")
                   {
                       return PartialView("Content/BrandSelectorBlock", settingsPage.GreenLanguageSelector);
                   }
                   else if (brand == "rockfon")
                   {
                       return PartialView("Content/BrandSelectorBlock", settingsPage.RockfonLanguageSelector);
                   }
                   else if (brand == "rockpanel")
                   {
                       return PartialView("Content/BrandSelectorBlock", settingsPage.RockpanelLanguageSelector);
                   }
                   else if (brand == "lapinus")
                   {
                       return PartialView("Content/BrandSelectorBlock", settingsPage.LapinusLanguageSelector);
                   }
                   else if (brand == "language")
                   {
                       return PartialView("Content/LanguageSelectorBlock", settingsPage.LanguageSelector);
                   }
               }
           }

           return null;
       }
   }

…次に、ビュー BrandSelectorBlock を使用します。

C#
@model BrandSelectorBlock
@{
    var list = Model.Continents;
    var tabs = list.Item1;
    var tabCount = "has-" + tabs.Count() + "-tabs";
    var NoEurope = string.IsNullOrWhiteSpace(tabs.Where(x => x.Equals(Continents.Europe)).FirstOrDefault());
    TempData.AddStyles(OrganismStyle.O23Tabs);
    TempData.AddOrganismScripts(ViewScript.O23TabsView);
}

<header class="modal-multi-selector__header">
    <div class="container">
        <button type="button" class="modal-multi-selector__close js-lang__close"></button>

        @if (!string.IsNullOrWhiteSpace(Model.BrandLogo))
        {
            <figure class="modal-multi-selector__logo">
                
                    @SvgHelper.GetSVG(Model.BrandLogo)
                
            </figure>
        }
        <div class="modal-multi-selector__description">
            <p class="body-text-2">@Html.ExtendedPropertyFor(x => x.BodyText)</p>
            <p class="body-text-2 is-hidden">@Html.ExtendedPropertyFor(x => x.VisitorMessage)</p>
        </div>
        <h3 class="modal-multi-selector__title">@Html.ExtendedPropertyFor(x => x.Heading)</h3>
    </div>
    <div class="O23-tabs @tabCount is-filter">
        <nav class="O23-tabs__nav">
            <div class="container">
                <ul class="O23-tabs__list is-hidden-sm">
                    @for (var i = 0; i < tabs.Count(); i++)
                    {
                        var tab = tabs.ElementAt(i);
                        var isActive = "";
                        if (tab.ToLower().Equals(Continents.Europe.ToLower()) || (NoEurope && i == 0))
                        {
                            isActive = "is-active";
                        }
                        <li class="O23-tabs__item @isActive">
                            <span>@tab</span>
                        </li>
                    }

                    <li class="O23-tabs__list__indicator"></li>
                </ul>

                <div class="O23-tabs__select is-hidden-md">
                    <div class="select">
                        <div class="select__wrap">
                            <div class="select__wrap__elem">
                                <select class="select__elem js-select" name="countries">
                                    @for (var i = 0; i < tabs.Count(); i++)
                                    {
                                        var tab = tabs.ElementAt(i);
                                        var selected = "";
                                        if (tab.Equals(Continents.Europe) || (NoEurope && i == 0))
                                        {
                                            selected = "selected";
                                        }
                                        <option @selected value="@tab.ToLower()">@tab</option>
                                    }
                                </select>
                            </div>

                            
                                @SvgHelper.GetSVG("small-arrow-down")
                            
                        </div>
                    </div>
                </div>
            </div>
        </nav>
    </div>
</header>

<div class="modal-multi-selector__content">
    <div class="modal-multi-selector__content__inner">
        <div class="container">
            <ul class="modal-multi-selector__list">
                @foreach (var langItem in list.Item2)
                {
                    <li class="modal-multi-selector__item" data-country-code="@langItem.CountryCode.ToLower()" data-filter="@langItem.Continent.ToLower()"><a href="@langItem.Link">@langItem.DisplayString</a></li>
                }
            </ul>
        </div>
    </div>
</div>

そして、LanguageSelectorBlock:

C#
@model LanguageSelectorBlock
@{
    var list = Model.Continents;
    var tabs = list.Item1;
    var tabCount = "has-" + tabs.Count() + "-tabs";
    TempData.AddOrganismScripts(ViewScript.O23TabsView);
}
<header class="modal-multi-selector__header">
    <div class="container">
        <button type="button" class="modal-multi-selector__close js-lang__close"></button>

        @if (!string.IsNullOrWhiteSpace(Model.BrandLogo))
        {
            <figure class="modal-multi-selector__logo">
                
                    @SvgHelper.GetSVG(Model.BrandLogo)
                
            </figure>
        }

        <h3 class="modal-multi-selector__title">@Model.Heading</h3>
    </div>

    <div class="O23-tabs @tabCount is-filter">
        <nav class="O23-tabs__nav">
            <div class="container">
                <ul class="O23-tabs__list is-hidden-sm">
                    @for (var i = 0; i < tabs.Count(); i++)
                    {
                        var tab = tabs.ElementAt(i);
                        var isActive = "";
                        if (i == 0)
                        {
                            isActive = "is-active";
                        }
                        <li class="O23-tabs__item @isActive">
                            <span>@tab</span>
                        </li>
                    }

                    <li class="O23-tabs__list__indicator"></li>
                </ul>

                <div class="O23-tabs__select is-hidden-md">
                    <div class="select">
                        <div class="select__wrap">
                            <div class="select__wrap__elem">
                                <select class="select__elem js-select" name="countries">
                                    @for (var i = 0; i < tabs.Count(); i++)
                                    {
                                        var tab = tabs.ElementAt(i);
                                        var selected = "";
                                        if (i == 0)
                                        {
                                            selected = "selected";
                                        }
                                        <option @selected value="@tab.ToLower()">@tab</option>
                                    }
                                </select>
                            </div>

                            
                                @SvgHelper.GetSVG("small-arrow-down")
                            
                        </div>
                    </div>
                </div>
            </div>
        </nav>
    </div>

</header>

<div class="modal-multi-selector__content">
    <div class="modal-multi-selector__content__inner">
        <div class="container">
            <ul class="modal-multi-selector__list">

                @foreach (var langItem in list.Item2)
                {
                    var active = langItem.CountryCode.Equals(ContentLanguage.PreferredCulture.Name, StringComparison.OrdinalIgnoreCase)
                    ? "is-active" : "";
                    <li class="modal-multi-selector__item @active" data-filter="@langItem.Continent.ToLower()"><a href="@langItem.Link">@langItem.DisplayString</a></li>
                }
            </ul>

            <div class="modal-multi-selector__more">
                @Html.ExtendedPropertyFor(x => x.MoreInfoButton)
            </div>
        </div>
    </div>
</div>

私が試したこと:

私は Html.PartialAsync と Html.RenderPartialAsync メソッドを使用しようとしましたが、Html.RenderAction の代わりに似ていないため、機能しませんでした。

解決策 1

.NET Core/5/6 では、 RenderAction メソッドは「ビュー コンポーネント」に置き換えられました。
ASP.NET Core でコンポーネントを表示する | Microsoft Docs[^]

解決策 2

名前空間 Microsoft.AspNetCore.Mvc.Rendering で HtmlHelperViewExtensions.RenderAction() を使用できないのはなぜですか?

[ad_2]

コメント

タイトルとURLをコピーしました