C# 将Google AMP(加速移动页面)应用于ASP.NET核心站点

C# 将Google AMP(加速移动页面)应用于ASP.NET核心站点,c#,asp.net,asp.net-core,amp-html,C#,Asp.net,Asp.net Core,Amp Html,我正在尝试用ASPNET核心MVC创建一个AMP页面。我找不到很多文件。对于ASPNET,建议使用DisplayModes创建一个Google-AMP显示器。然而,ASPNet核心不支持DisplayModes,我正试图找到一种解决方法。如有任何建议,将不胜感激 @model Models.Article @{ 布局=空; } body{-webkit动画:-amp start 8s步骤(1,结束)0s1正常两者;-moz动画:-amp start 8s步骤(1,结束)0s1正常两者;-ms动画

我正在尝试用ASPNET核心MVC创建一个AMP页面。我找不到很多文件。对于ASPNET,建议使用
DisplayModes
创建一个Google-AMP显示器。然而,ASPNet核心不支持
DisplayModes
,我正试图找到一种解决方法。如有任何建议,将不胜感激

@model Models.Article
@{
布局=空;
}
body{-webkit动画:-amp start 8s步骤(1,结束)0s1正常两者;-moz动画:-amp start 8s步骤(1,结束)0s1正常两者;-ms动画:-amp start 8s步骤(1,结束)0s1正常两者;动画:-amp start 8s步骤(1,结束)0s1正常两者}@@@webkit关键帧-amp start{从{可见性:隐藏}到{可见性:可见}@@@moz关键帧-amp start{from{可见性:隐藏}到{可见性:可见}@@@ms关键帧-amp start{从{可见性:隐藏}到{可见性:可见}@@@o-关键帧-amp start{从{可见性:隐藏}到{可见性:可见}@@关键帧-amp start{从{可见性:隐藏}到{可见性:可见}}
身体{
-webkit动画:无;
-moz动画:无;
-ms动画:无;
动画:无
}
@DisplayFor(model=>model.Title)
@Convert.ToDateTime(Model.PublishedDate).ToString(“dddd,MMMM d,yyyy”)

有几种方法可以实现类似的效果。一种可能是根据路线动态更改布局,即使用模板X表示AMP或Y表示AMP或Y

一个更强大的解决方案是视图位置扩展器。这通常也被认为是显示模式的后续。视图位置扩展器基本上是一种机制,允许您对Razor引擎将在其中查找视图的物理位置进行后处理。因此,您可以使用它有条件地修改r展开视图所在的路径

在您的情况下,您可能希望更改行为,以便在通过AMP访问您的站点时,Razor应该首先查找
.AMP.cshtml
,然后再返回到
.cshtml

为了做到这一点,您必须实现。在中,您必须检测您是否处于AMP模式;在中,您可以修改视图位置

这可能看起来有点像这样(未经测试,作为如何实现这一点的想法):


然后您只需要为AMP创建备用视图。

有几种方法可以实现类似的效果。一种可能是根据路线动态更改布局,即使用模板X表示AMP,或者使用模板Y表示AMP

一个更强大的解决方案是视图位置扩展器。这通常也被认为是显示模式的后续。视图位置扩展器基本上是一种机制,允许您对Razor引擎将在其中查找视图的物理位置进行后处理。因此,您可以使用它有条件地修改r展开视图所在的路径

在您的情况下,您可能希望更改行为,以便在通过AMP访问您的站点时,Razor应该首先查找
.AMP.cshtml
,然后再返回到
.cshtml

为了做到这一点,您必须实现。在中,您必须检测您是否处于AMP模式;在中,您可以修改视图位置

这可能看起来有点像这样(未经测试,作为如何实现这一点的想法):


然后,您只需要为AMP.创建备用视图。

嘿,这是一个很好的答案!我只想补充一点,我使用的是
asp.net core 2.1
,在
mvcopions
中没有找到
ViewLocationExpanders
属性;相反,我使用
services.Configure>配置了我的
ViewLocationExpander
(o=>{o.ViewLocationExpanders.Add(new MyViewLocationExpander();});
@MartinShishkov哦,你是对的,它们是剃须刀选项的一部分。不知道为什么我认为它们是mvcopions的一部分。在我的回答中更正了这一点,谢谢!)嘿,这是一个很好的答案!我只想补充一点,我使用的是
asp.net core 2.1
,在
MvcOptions
中没有找到
ViewLocationExpanders
属性;相反,我使用
服务配置了我的
ViewLocationExpander
。配置(o=>{o.ViewLocationExpanders.add(新建MyViewLocationExpander();});
@MartinShishkov哦,你说得对,它们是剃须刀选项的一部分。不知道为什么我认为它们是MVCOPIONS的一部分。在我的回答中更正了这一点,谢谢!)
@model Models.Article
@{
    Layout = null;
}

<!doctype html>
<html amp>
    <head>
        <meta charset="utf-8">
        <link rel="canonical" href="/article.cshtml">
        <link rel="amphtml" href="/article.amp.cshtml">
        <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
        <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style>
        <noscript>
        <style amp-boilerplate>
          body {
          -webkit-animation: none;
          -moz-animation: none;
          -ms-animation: none;
          animation: none
          }
    </style></noscript>
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    </head>
    <body>
        <article>
            <h2>@Html.DisplayFor(model => model.Title)</h2>
            <div>@Convert.ToDateTime(Model.PublishedDate).ToString("dddd, MMMM d, yyyy")</div>
        </article>    
    </body>
</html>
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;
        }
    }
}
services.AddMvc()
    .AddRazorOptions(options =>
    {
        options.ViewLocationExpanders.Add(new AmpViewLocationExpander());
    });