ASP.Net HTTP2 PushPromise速度很慢

编程入门 行业动态 更新时间:2024-10-21 20:38:17
本文介绍了ASP.Net HTTP2 PushPromise速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用"PushPromise" .NET 4.6.1实现Http2推送服务器功能,为此,我为Razor提供了"html扩展名"(我们未实现MVC,仅使用Razor引擎来构建页面).

I'm trying to implement the Http2 push server functionality using "PushPromise" .NET 4.6.1, for this I have a "html extension" with Razor (we did not implement MVC only used the Razor engine to build the pages).

public static IHtmlString PushPromiseStylesheet(this HtmlHelper htmlHelper, string src, bool addDomElement = true) { var context = HttpContext.Current; var path = System.Web.Optimization.Styles.Url(src).ToString(); var headers = new NameValueCollection { { "accept-encoding", context.Request.Headers["accept-encoding"] } }; context.Response.PushPromise(path, "GET", headers); var styleElement = $"<link rel=\"preload\" href=\"{path}\" as=\"style\">"; return new HtmlString(addDomElement ? styleElement : String.Empty); } public static IHtmlString PushPromiseJavascript(this HtmlHelper htmlHelper, string src) { var context = HttpContext.Current; var path = System.Web.Optimization.Scripts.Url(src).ToString(); var headers = new NameValueCollection { { "accept-encoding", context.Request.Headers["accept-encoding"] } }; context.Response.PushPromise(path,"GET",headers); var javascriptElement = $"<link rel=\"preload\" href=\"{path}\" as=\"script\">"; return new HtmlString(javascriptElement); } public static IHtmlString PushPromiseImage(this HtmlHelper htmlHelper, string src, bool addDomElement = false) { var context = HttpContext.Current; var path = System.Web.Optimization.Scripts.Url(src).ToString(); var headers = new NameValueCollection { { "accept-encoding", context.Request.Headers["accept-encoding"] } }; context.Response.PushPromise(path,"GET",headers); var imgElement = $"<link rel=\"preload\" href=\"{path}\">"; return new HtmlString(addDomElement ? imgElement : String.Empty); } public static IHtmlString PushPromiseWebFont(this HtmlHelper htmlHelper, string src, string type = null) { var context = HttpContext.Current; var path = System.Web.Optimization.Scripts.Url(src).ToString(); type = string.IsNullOrWhiteSpace(type) ? "font/woff2" : type; var headers = new NameValueCollection { { "accept-encoding", context.Request.Headers["accept-encoding"] } }; context.Response.PushPromise(path, "GET", headers); var fontElement = $"<link rel=\"preload\" href=\"{path}\" as=\"font\" type=\"{type}\"> "; return new HtmlString(fontElement); }

在<中页面的head>:

And in the < head > of the page:

<head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> <title>@Page.Title</title> @Html.PushPromiseStylesheet("~/css/app/fonts.css") @Html.PushPromiseStylesheet("~/css/landing.base.css") @Html.PushPromiseStylesheet("~/css/landing.style.css") @Html.PushPromiseImage("/assets/img/bg-ofertas.jpg") @Html.PushPromiseImage("/assets/img/landings/v2/iphone.png") @Html.PushPromiseImage("/assets/img/landings/v2/ipad-new.png") @Html.PushPromiseImage("/assets/img/landings/v2/macbook-new.png") @Html.PushPromiseWebFont("/assets/fonts/CredimejoraIcons.woff2?miydpz") @Html.PushPromiseWebFont("/assets/fonts/centrale_sans_regular_italic-webfont.woff2") @Html.PushPromiseWebFont("/assets/fonts/centrale_sans_bold_italic-webfont.woff2") @Html.PushPromiseWebFont("/assets/fonts/centrale_sans_regular-webfont.woff2") @Html.PushPromiseWebFont("/assets/fonts/centrale_sans_bold-webfont.woff2") @Html.PushPromiseJavascript("/assets/js/public/libs/jquery.inputmask.bundle-3.3.6.2.js")

我已经托管了不使用PushPromise"这个项目(credimejorademo),在另一个领域中,我实现了PushPromise(aquituinmueble):

I have hosted the same project "without using PushPromise" (credimejorademo), and in another domain I have implemented PushPromise (aquituinmueble):

www.webpageare/.php?tests = 170609_PD_e32f303e034aef3fef7140fef181a738,170609_FK_7d538ecdacf071cce320ad14bf97414c

推荐答案

我在这里提到了几件事.首先,尽管这似乎是违反直觉的,但是您最好不要推动-everything-.我见过的测试数量有限(例如: www.smashingmagazine/2017/04/guide-http2-server-push/)建议仅推送css可能是提高性能的最佳方法,尽管值得在单个站点上试用.

Just a couple of things I'd mention here. Firstly, although this may seem counter-intuitive, you're probably best off not pushing -everything-. The limited amount of testing I've seen (eg: www.smashingmagazine/2017/04/guide-http2-server-push/) suggests that pushing css only is probably the best way to improve performance, although it's worth playing around with on an individual site basis.

另一件事是,您实现PushPromise的方式意味着IIS仅在开始处理页面后才收到该请求.也就是说,您几乎在页面通过标准请求推送的同时请求推送.

The other thing is that the way you are implementing PushPromise means that IIS is only getting that request once it's already started processing the page. ie, you're requesting the push at more or less the same time the page would have requested it via a standard .

希望这会有所帮助.

更多推荐

ASP.Net HTTP2 PushPromise速度很慢

本文发布于:2023-11-01 20:12:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:速度很慢   Net   ASP   PushPromise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!