HtmlWebViewSource BaseUrl Doesn't Seem to Function Correctly

NickPNickP Member ✭✭

I'm trying to allow my web viewer to load some local .css and .js files
Using the BaseUrl member of HtmlWebViewSource and DepenencyService to set a local file path.

By debugging I've confirmed that the BaseUrl is being set to 'file:///android_asset'
However I can via remote debugger my resources are failing to load as they are being loaded by 'file:///test.css' rather than this base.
Hard coding the links to 'file:///android_asset/test.css' succeeds in loading the resource

Why is BaseUrl not causing relative filepaths to point to android_asset?

Tagged:

Best Answer

  • NickPNickP ✭✭
    Accepted Answer

    I was actually just about to post the solution @jezh , I'll go into some more detail about the problem and it's solution.

    I'm trying to use a WebView to display a local webpage, with locally stored .css and .js files

    I wanted the following HTML element's href to refer to 'file:///android_asset/Content/style.css'

    <link rel="stylesheet" href="/Content/style.css">

    Despite defining HtmlWebViewSource.BaseUrl as 'file:///android_asset/', the shown would point to 'file:///Content/style.css'. So that it was unable to grab any file from android_asset as a relative path.

    It drove me absolutely crazy, until I found this post: https://stackoverflow.com/questions/23438416/why-is-httpclient-baseaddress-not-working

    It seems there is some very non-intuitive, undocumented behavior with the BaseUrl

    The BaseUrl property must end with a slash IE 'file:///android_asset/'
    The relative link in your document must not begin with a slash IE href="Content/style.css"

    I'm not sure why, but any other permutation causes it to fail quietly

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post more details about this problem?

  • NickPNickP Member ✭✭
    Accepted Answer

    I was actually just about to post the solution @jezh , I'll go into some more detail about the problem and it's solution.

    I'm trying to use a WebView to display a local webpage, with locally stored .css and .js files

    I wanted the following HTML element's href to refer to 'file:///android_asset/Content/style.css'

    <link rel="stylesheet" href="/Content/style.css">

    Despite defining HtmlWebViewSource.BaseUrl as 'file:///android_asset/', the shown would point to 'file:///Content/style.css'. So that it was unable to grab any file from android_asset as a relative path.

    It drove me absolutely crazy, until I found this post: https://stackoverflow.com/questions/23438416/why-is-httpclient-baseaddress-not-working

    It seems there is some very non-intuitive, undocumented behavior with the BaseUrl

    The BaseUrl property must end with a slash IE 'file:///android_asset/'
    The relative link in your document must not begin with a slash IE href="Content/style.css"

    I'm not sure why, but any other permutation causes it to fail quietly

Sign In or Register to comment.