looking for some solutions? You are welcome.

SOLVED: Applying google AMP to MVC core application (alternative to Display modes for MVC core)

Renu:
I have an application which uses mobile views and desktop views as different html pages. Now I am moving that to Asp.Net core.I'm trying to create an AMP page with ASPNET Core MVC.

public class AmpViewLocationExpander : IViewLocationExpander { private const string ValueKey = "ampmode";

        public void PopulateValues(ViewLocationExpanderContext context)
        {
            // magic utility method that determines whether this is within an AMP context
            var isAmp = context.ActionContext.HttpContext.IsAmp();

            // persist the value on the context to allow the cache to consider this
            context.Values[ValueKey] = isAmp.ToString();
        }

        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context,
            IEnumerable<string> viewLocations)
        {
            // when in AMP mode
            if (context.Values.TryGetValue(ValueKey, out var isAmpValue) && isAmpValue == "True")
            {
                return ExpandAmpViewLocations(viewLocations);
            }

            // otherwise fall back to default locations
            return viewLocations;
        }

        private IEnumerable<string> ExpandAmpViewLocations(IEnumerable<string> viewLocations)
        {
            foreach (var location in viewLocations)
            {
                // yield the AMP version first
                yield return location.Replace("{0}", "{0}.amp");

                // then yield the normal version as a fallback
                yield return location;
            }
        }        
    }

I searched for this and implemented as in the link given :- [Apply Amp using MVC Core]. But it is showing "'HttpContext' does not contain a definition for 'IsAmp' and no accessible extension method 'IsAmp' accepting a first argument of type 'HttpContext' could be found (are you missing a using directive or an assembly reference?)" error.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent