Webview Source issue
We are encountering an issue where the WebView in our Android app is automatically appending "file:///android_asset/" to the URL, causing the webpage not to display correctly on our device.
Please help us how to solve this issue.
.NET MAUI
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,921 Reputation points • Microsoft Vendor
2024-02-05T06:45:44.85+00:00 For further investigation, could you please provide the following information?
- What is your MAUI version?
- Could you please provide steps to reproduce this issue and update it to the issue?
You could refer to Guidelines for posting a quality question on Microsoft Q&A for more details.
-
Yogita Magdum 5 Reputation points
2024-02-05T13:48:11.9666667+00:00 Hello, MAUI version-8.0.6
Issue:
We are binding the url with below source:
https://www.umrahme.com/hotel/en-eu/details?checkin=2024-02-12&checkout=2024-02-13&destinationId=235565&destination=Makkah,%20Saudi%20Arabia&occupancy=1_2_&orderby=price&sortby=asc&starrating=[5]&hotelId=640803&hotelname=Makkah%20Al%20Aziziah
but when navigating attach the file:///android_asset/. -
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,921 Reputation points • Microsoft Vendor
2024-02-06T02:50:02.6133333+00:00 I have tested with the URL you provided, and the WebView didn't automatically add
"file:///android_asset/"
when navigating.Could you please provide the exact steps to reproduce into the issue?
-
Yogita Magdum 5 Reputation points
2024-02-08T13:25:55.6+00:00 Design:
<WebView x:Name="webView" Navigated="WebView_Navigated" Navigating="WebView_Navigating" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Source="{Binding Source,Mode=TwoWay}" UserAgent="UME-WEB" Grid.Row="1" />
Design CS File:
public HotelDetailPage(System.Collections.Specialized.NameValueCollection queryStringDict) { InitializeComponent(); this.BindingContext = new HotelDetailsViewModel(this.Navigation, queryStringDict); } private async void WebView_Navigating(object sender, WebNavigatingEventArgs e) { Uri uri = new Uri(e.Url); string query = uri.Query; var queryStringData = System.Web.HttpUtility.ParseQueryString(query); if (uri.Segments.Last() == "listing.html" || uri.Segments.Last() == "listing") { e.Cancel = true; var pageToFind = Application.Current.MainPage.Navigation.NavigationStack.FirstOrDefault(page => page.GetType() == typeof(HotelListingPage)); if (pageToFind != null) { await this.Navigation.PopAsync(true); } else { await this.Navigation.PushAsync(new HotelListingPage(queryStringData)); var currentPage = Navigation.NavigationStack.FirstOrDefault(page => page.GetType() == typeof(HotelDetailPage)); if (currentPage != null) { Navigation.RemovePage(currentPage); } } } if (uri.Segments.Last() == Constants.Traveler) { e.Cancel = true; if (!App.IsSearchModify) { var bindingContext = this.BindingContext as HotelDetailsViewModel; bindingContext.AssignDefaultValue(); } await this.Navigation.PushAsync(new GuestDetailsPage(queryStringData)); } }
Viewmodel:
source = queryStringDict.GetSource($"{Preferences.Get("webdomain", "")}hotel/{App.Language}-{App.CountryCode}/details?"); ;
where querystringDict is NameValueCollection and GetSource is extension method:public static Uri GetSource(this System.Collections.Specialized.NameValueCollection queryStringDict, string url) { if (queryStringDict.AllKeys.Contains(Constants.DistinationKey)) { queryStringDict.Remove(Constants.DistinationKey); } var queryString = string.Join("&", queryStringDict.AllKeys.Select(kv => ($"{kv}={queryStringDict[kv]}"))); if (queryStringDict.AllKeys.Contains("ismobileview")) { return new Uri($"{url}{queryString}"); } if(url.Contains("?")) { return new Uri($"{url}{queryString}&ismobileview=true"); } else { return new Uri($"{url}?{queryString}&ismobileview=true"); } }
where url contain arabic string then it gives also same issue.
I am navigating below url : https://www.umrahme.com/hotel/en-sa/details?checkin=2024-02-19&checkout=2024-02-20&destinationId=235565&occupancy=1_2_&orderby=price&sortby=asc&ismobileview=true&starrating=[4,5]&hotelId=815552&hotelname=Park%20Inn%20By%20Radisson%20Makkah%20Aziziyah then once again navigating excuted with following url:
file:///android_asset/https://www.umrahme.com/hotel/en-sa/details?checkin=2024-02-19&checkout=2024-02-20&destinationId=235565&occupancy=1_2_&orderby=price&sortby=asc&ismobileview=true&starrating=[4,5]&hotelId=815552&hotelname=Park%20Inn%20By%20Radisson%20Makkah%20Aziziyah -
Yogita Magdum 5 Reputation points
2024-02-08T13:50:07.4066667+00:00 <WebView x:Name="webView" Navigated="WebView_Navigated" Navigating="WebView_Navigating" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Source="{Binding Source,Mode=TwoWay}" " Grid.Row="1" />
use this design file -
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,921 Reputation points • Microsoft Vendor
2024-02-09T02:40:32.7766667+00:00 This error could be caused using URIs, because during the conversion of the URI of the .NET URI to the URI of Android Java URI, the link to the URI is considered to be an internal resource file of Android, so this prefix will appear after the conversion.
For web-related data processing, HTTPClient is the most recommended approach.
In addition, since this issue is highly related to specific use cases, it is recommended that you create a support ticket to get one-on-one technical support.
-
Yogita Magdum 5 Reputation points
2024-02-09T07:07:33.5733333+00:00 why this url append assets path?
-
Yogita Magdum 5 Reputation points
2024-02-09T07:39:58.39+00:00 I apologize for the confusion. It seems I mistakenly provided an dev environment and prod environment 2 different URL in my previous message but correct url is following:
https://www.umrahme.com/hotel/en-sa/details?checkin=2024-02-19&checkout=2024-02-20&destinationId=235565&occupancy=1_2_&orderby=price&sortby=asc&ismobileview=true&starrating=[4,5]&hotelId=815552&hotelname=Park%20Inn%20By%20Radisson%20Makkah%20Aziziyah
and navigating event second time append string and generate following url :
file:///android_asset/https://www.umrahme.com/hotel/en-sa/details?checkin=2024-02-19&checkout=2024-02-20&destinationId=235565&occupancy=1_2_&orderby=price&sortby=asc&ismobileview=true&starrating=[4,5]&hotelId=815552&hotelname=Park%20Inn%20By%20Radisson%20Makkah%20Aziziyah -
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,921 Reputation points • Microsoft Vendor
2024-02-23T07:29:39.2533333+00:00 why this url append assets path?
As mentioned above, this is an error when converting a .Net URI to an Android URI. To solve this problem, you could use a pure string operation and return a string. MAUI's WebView is automatically converted to the corresponding URI when it receives an HTTP link string.
Despite the
Source
property being of typeWebViewSource
, the property can be set to a string-based URI. This is because .NET MAUI includes a type converter, and an implicit conversion operator, that converts the string-based URI to aUrlWebViewSource
object.> Quated from Display a web page -
Anila Mathew 30 Reputation points
2024-07-05T12:18:51.0333333+00:00 Hi @Yogita Magdum
Did u find the solution? I am having this issue in Android 14 and in Android 12, it is working fine.. Do u have any suggestions on the same?? -
zhang yu hui 0 Reputation points
2024-07-18T06:17:51.8633333+00:00 @Anila Mathew ,
var url = new Uri(source);webView.Source = url;
it's worked after assign the url like this. hope this help.
Sign in to comment