Amir Abdollahi
Amir Abdollahi

Reputation: 65

How to implement MVVM Light INavigationService in a DotVVM project

I want to implement the MVVM Light INavigationService interface in a DotVVM project; but I don't know how to do that. The most important method that I need to implement is NavigateTo(string pageKey) method.

I am using a SpaContentPlaceHolder in my MasterPage and I want to change the content (RouteName) of the SpaContentPlaceHolder by calling the NavigateTo method.

Upvotes: 2

Views: 226

Answers (1)

Tomáš Herceg
Tomáš Herceg

Reputation: 1640

If you are in the viewmodel, you can just call Context.RedirectToRoute("YourRoute", new { Param1 = something }).

If you want to redirect from a different place, the easiest way is to create INavigationService interface and implement it to call the method on IDotvvmRequestContext (which is already registered in the ASP.NET Core dependency injection container):

public interface INavigationService 
{
    void NavigateTo(string routeName, object routeParameters);
}

public class DotvvmNavigationService 
{
    private IDotvvmRequestContext context;

    public DotvvmNavigationService(IDotvvmRequestContext context) {
        this.context = context;
    }

    public void NavigateTo(string routeName, object routeParameters) {
        this.context.RedirectToRoute(routeName, routeParameters);
    }
}

Then, you can just register the implementation as a scoped dependency in Startup.cs and you should be able to get it anywhere you need.

services.AddScoped<DotvvmNavigationService>();

Upvotes: 2

Related Questions