Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Overload UriHelper to forceLoad the page even if it's not a Blazor defined Route. #1154

Closed
wants to merge 2 commits into from
Closed

Overload UriHelper to forceLoad the page even if it's not a Blazor defined Route. #1154

wants to merge 2 commits into from

Conversation

vertonghenb
Copy link
Contributor

@vertonghenb vertonghenb commented Jul 19, 2018

PR issued to solve

  • Add UrlHelper.NavigateTo overload that forces a full page reload #979
  • Handle FileResult from controller service #962
  • Combine MVC razor pages with Blazor pages #1069
  • Helps with Routing enhancements #293

Basically when you have a MVC Server that serves the Blazor Client and on the other hand sends File Downloads (for example). It was not possible to load the MVC Uri outside of Blazor's internal routing.
With this PR, it's now possible to use the following block of code to load a Uri outside of Blazor's routing:

@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper
<button onclick=@GoToMvcRoute>Navigate to Controller Action!</button>
<button onclick=@DontGoToMvcRoute>Don't Navigate to Controller Action!</button>

@functions {
    void GoToMvcRoute()
    {
        UriHelper.NavigateTo("/controller/action", forceLoad: true);

    }
    void DontGoToMvcRoute()
    {
        UriHelper.NavigateTo("/controller/action");

    }
}

Tests
Not really sure how to test this in the current suite, maybe add an additional AspNetHosted project with
a server that also serves via MVC? I simply tested on "my machine" to make sure it's loading the page.

Caveats
The mapping of the controllers still have to happen on Startup.cs (which is normal):

            app.UseMvcWithDefaultRoute();
            app.UseBlazor<Client.Program>();

Impact
No impact on current usages of IUriHelper (default parameter)

If you know a better name for the parameter, I'm all ears!

  • byPassInternalRouting?

@chucker
Copy link

chucker commented Jul 19, 2018

/// <param name="forceLoad">Indicator to force load the URI, even if it's not a blazor route.</param>

I definitely find forceLoad too vague.

A few candidates:

  • skipRouteValidation? I find it clearer, if a little awkward. (Sort of similar to your suggestion of byPassInternalRouting.)
  • useJavaScriptRouting? This is perhaps a little misleading, but makes it clearer that 1) the normal routing has nothing to do with JS (and implicitly with Blazor instead), and 2) this is the traditional browser-side stuff.

@ghost
Copy link

ghost commented Jul 19, 2018

Not to be nitpicky, but if "byPassInternalRouting" is used, could we correctly name it "bypassInternalRouting" bypass is one word.

@Andrzej-W
Copy link

Where can we use enhancement like this? Probably there are two places: UriHelper.NavigateTo and HTML <a> element. If so, the best name in my opinion is serverSideUrl or simply serverUrl.

For completeness NavLink component Blazor\src\Microsoft.AspNetCore.Blazor\Routing\NavLink.cs should be enhanced also. Then we need some changes in Blazor router. Unfortunately I have no idea what have to be changed and where exactly.

@vertonghenb
Copy link
Contributor Author

I was also thinking to simply not use the NavigateTo and create a new property
Location with a getter and setter which triggers a JS.Interop call like:

location.href = uri;

This is basically what we're doing setting the location, since it is a hard reload of the page.

@vertonghenb
Copy link
Contributor Author

vertonghenb commented Jul 23, 2018

@SteveSandersonMS any opinion on this? So I can prepare it for the 0.5.0 if you'd like.

@SteveSandersonMS
Copy link
Member

@vertonghenb Thanks for contributing this! We won't get it in in time for 0.5.0 (there's quite a bit to review and some API decisions to make) but we should be able to merge to master soon after 0.5.0 ships. Hope that's OK!

@vertonghenb
Copy link
Contributor Author

Sure, if the API decision is made, i'll assist further.

@plasticalligator
Copy link

plasticalligator commented Aug 6, 2018

From my personal experience, location.href = "xyz"; will occasionally outright not work, depending on the browser, build, security settings, if you're running on localhost or production, and how pissed off the cosmic space gnomes are at you on a particular thursday.

Using location.reload() might be a better cross-experience solution like so.

<a onclick=@(()=>Navigate("/login")) href="#">Login</a>
        window.Refresh = (a) => {
            location.reload();
            return true;
        }
async void Navigate(string uri)
{
    UriHelper.NavigateTo(uri);
    await JSRuntime.Current.InvokeAsync<bool>("Refresh");
}

@vertonghenb
Copy link
Contributor Author

@SteveSandersonMS something for 0.6.0?

@DocBrown101
Copy link

Anything new yet? Will it be included in version 0.7.0?

@SteveSandersonMS
Copy link
Member

Thanks again for this, @vertonghenb! It's now moved to dotnet/aspnetcore#4786, where it will be merged shortly.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
8 participants