Search code examples
c#razorlocalhostumbraco

Umbraco 10 : Detection of browser / userAgent on localhost


I have installed an instance V12 locally.
I'm trying to check for mobile and other devices

The error message im getting is this one:

An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately.

  • The name 'Request' does not exist in the current context

    • @if(Request.Browser.IsMobileDevice && !Request.UserAgent.Contains("iPad") && !Request.UserAgent.Contains("Tablet"))

My code :

@inherits Umbraco.Cms.Web.Common.Macros.PartialViewMacroPage;
@{
    var imageCrop = Model.MacroParameters["crop"].ToString();
}


<div class="heading">
        <div class="heading01">@Model.Content.Value("bannerTitle")</div>
        <div class="heading02">@Model.Content.Value("bannerSubtitle")</div>
</div>

@if(Model.Content.HasValue("videoLink"))
{
    <div class="play-video" data-fancybox href="@Model.Content.Value("videoLink")">
        <span>
            <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 17 21">              
            <polygon points="16.9,10.5 0.1,21 0.1,0 "/>
            </svg>
        </span>
    </div>
}

<div class="overlay"></div>

@if(Request.Browser.IsMobileDevice && !Request.UserAgent.Contains("iPad") && !Request.UserAgent.Contains("Tablet"))
    {
        <img src="@Model.Content.GetCropUrl("bannerImage", "mobile")" class="img-fluid" alt="@Model.Content.Value("altTag")" />
    }
    else 
    {
        if(imageCrop == "crop")
        {
            <img src="@Model.Content.GetCropUrl("bannerImage", "crop")" class="img-fluid" alt="@Model.Content.Value("altTag")" />
        }
        else
        {
            <img src="@Model.Content.GetCropUrl("bannerImage", "full")" class="img-fluid" alt="@Model.Content.Value("altTag")" />
        }
    }

Am I missing something?


Solution

  • So you're trying to access the Request object from within a razor view?

    Then I think the issue isn't really related with umbraco (which I don't really know) but with ASP.NET razor views in general.

    As the error says ("The name 'Request' does not exist in the current context") it doesn't know, what "Request" is. So you cannot access it directly. But any razor page gives you access to the HttpContext via its Context property (see here).

    So you can change

    @if(Request.Browser.IsMobileDevice ...
    

    to

    @if(Context.Request.Browser.IsMobileDevice ...