Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Prasant 10 posts 90 karma points
    Dec 20, 2022 @ 08:46
    Prasant
    0

    Request.Url.AbsoluteUri == (HttpContext.Current.Request.Host.GetLeftPart(UriPartial.Authority) + item.Url()) not working

    This not working in.net 7 Umbraco 11

         @if(Request.Url.AbsoluteUri ==(HttpContext.Current.Request.Host.GetLeftPart(UriPartial.Authority) + item.Url()))
        {
           <a href="@item.Url()" target="@item.Target" rel="@item.Noopener @item.Noreferrer">@item.Title</a>
         }
       @else
         {
    
       <a href="/" target="@item.Target" rel="@item.Noopener @item.Noreferrer">@item.Title</a>
    
        }
    

    enter image description here

  • Pasang Tamang 258 posts 458 karma points
    Dec 20, 2022 @ 10:11
    Pasang Tamang
    1

    Hi Prashant,

    The code you are using is for ASP.NET MVC. For ASP.NET Core or .NET 7 you have to use different approach. I am not sure what you are trying to achieve but below code example shows how you can retrieve URL

    @using Microsoft.AspNetCore.Http
    @inject IHttpContextAccessor HttpContextAccessor
    @{
        Uri? uriValue = HttpContextAccessor.HttpContext?.Request.GetTypedHeaders().Referer;
    }
    

    If you can explain me what you are trying to achieve then I could suggest you alternative code also.

    Thanks

    Regards, Pasang

  • Prasant 10 posts 90 karma points
    Dec 23, 2022 @ 03:17
    Prasant
    0

    I am using Umbraco 11 and .net 7 to achieve the page that equals to request page for Navbar. Please see the code below.

     @{
                            var umbNav = home.Value<IEnumerable<UmbNavItem>>("menu");
                            if(umbNav != null)
                            {
    
                            foreach (var item in umbNav)
                                {
                                    if(@item.Children.Count() == 0 )
                                        {
                                                <li>
                                                @if(HttpContext.Current.Request.Url.AbsoluteUri == (HttpContext.Current.Request.Host.GetLeftPart(UriPartial.Authority) + item.Url()))
                                                {
                                                        <a href="@item.Url()" target="@item.Target" rel="@item.Noopener @item.Noreferrer">@item.Title</a>
                                                }
                                                else
                                                {
    
                                                     <a href="/" target="@item.Target" rel="@item.Noopener @item.Noreferrer">@item.Title</a>
    
                                                }
                                                </li>
                                        }
                                }
                            }
                        }
    
  • Pasang Tamang 258 posts 458 karma points
    Dec 23, 2022 @ 03:49
    Pasang Tamang
    100

    Hi Prasant,

    Looking into your code, you are using UmbNav package (Please correct if I am wrong). With UmbNav, there are various ways to check active page. Please check below code examples how I am using them

    First Solution:

            @{
                var umbNav = homePage.Value<IEnumerable<UmbNavItem>>("menu");
                foreach (var item in umbNav)
                {
                    <li class="nav-item">
                        <a class="nav-link px-lg-3 py-3 py-lg-4 @(item.IsActive?"active": "")" href="@(item.Url())">@(item.Title) @item.IsActive</a>
                    </li>
                }
            }
    

    Second Solution:

                @{
                    var umbNav = homePage.Value<IEnumerable<UmbNavItem>>("menu");
                    foreach (var item in umbNav)
                    {
                        <li class="nav-item">
                            <a class="nav-link px-lg-3 py-3 py-lg-4 @(item.Udi.Guid == Model.Key?"active": "")" href="@(item.Url())">@(item.Title) @item.IsActive</a>
                        </li>
                    }
                }
    

    Third Solution:

            @{
                var umbNav = homePage.Value<IEnumerable<UmbNavItem>>("menu");
                foreach (var item in umbNav)
                {
                    <li class="nav-item">
                        <a class="nav-link px-lg-3 py-3 py-lg-4 @(item.Content.Id == Model.Id?"active": "")" href="@(item.Url())">@(item.Title) @item.IsActive</a>
                    </li>
                }
            }
    

    I hope you will find this helpful to you.

    Thanks

    /Pasang

  • Prasant 10 posts 90 karma points
    Dec 23, 2022 @ 04:23
    Prasant
    0

    Thanks, Sir, great help ☻

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies