{"id":104881,"date":"2021-02-19T07:00:00","date_gmt":"2021-02-19T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104881"},"modified":"2021-02-19T06:59:39","modified_gmt":"2021-02-19T14:59:39","slug":"20210219-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210219-00\/?p=104881","title":{"rendered":"How can I prevent a Windows Runtime WebView from loading any content beyond the initial request and its redirects?"},"content":{"rendered":"<p>Last time, we configured a Windows Runtime WebView control so that it <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210218-00\/?p=104879\"> loaded content only from the initial request, and all other requests were blocked<\/a>. However, we found that this meant that if the original request resulted in a redirect, we blocked the redirect.<\/p>\n<p>How can we allow the redirects?<\/p>\n<p>When the initial request is made, or when a redirect is followed, the WebView control raises the <code>Navigation\u00adStarting<\/code> event. We can listen to that event to follow the redirects.<\/p>\n<p>Start afresh with <a href=\"https:\/\/github.com\/microsoft\/Windows-universal-samples\/blob\/08d7459c552fe1065d3fe195b3fe2a5d7ec3b567\/Samples\/XamlWebView\/\"> the WebView sample<\/a> and make these changes.<\/p>\n<pre>    public <a href=\"https:\/\/github.com\/microsoft\/Windows-universal-samples\/blob\/08d7459c552fe1065d3fe195b3fe2a5d7ec3b567\/Samples\/XamlWebView\/cs\/Scenario1_NavToUrl.xaml.cs#L22\">Scenario1_NavToUrl<\/a>()\r\n    {\r\n        this.InitializeComponent();\r\n        <span style=\"color: blue;\">WebViewControl.WebResourceRequested += OnResourceRequested;<\/span>\r\n    }\r\n\r\n    <span style=\"color: blue;\">Uri allowedUri = null;\r\n\r\n    void OnResourceRequested(WebView sender,\r\n             WebViewWebResourceRequestedEventArgs e)\r\n    {\r\n        if (e.Request.RequestUri != allowedUri)\r\n        {\r\n            e.Response = new Windows.Web.Http.HttpResponseMessage(\r\n                             Windows.Web.Http.HttpStatusCode.NotFound);\r\n        }\r\n    }<\/span>\r\n\r\n    void WebViewControl_NavigationStarting(WebView sender,\r\n             WebViewWebResourceRequestedEventArgs e)\r\n    {\r\n        string url = UriToString(args.Uri);\r\n        AddressBox.Text = url;\r\n        <span style=\"color: blue;\">allowedUri = args.Uri;<\/span>\r\n        AppendLog($\"Starting navigation to: \\\"{url}\\\".\");\r\n        pageIsLoading = true;\r\n    }\r\n<\/pre>\n<p>This time, we update the <code>allowedUri<\/code> when a navigation starts, which happens either in the original navigation or as the result of a redirect.<\/p>\n<p>Note that <code>Navigation\u00adStarting<\/code> is also raised when the user triggers a navigation by clicking a link, or if script on the page triggers its own navigation. If you want to block those, you can listen for the <code>Navigation\u00adComplete<\/code> event and reject any subsequent navigations, or at least stop allowing them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Follow the bouncing redirect.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-104881","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Follow the bouncing redirect.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=104881"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104881\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=104881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}