[Static Commands] How to gain access to property "Context" in code-behind view models

I need to reload the page after setting the language, so I need access to the Context inside the static command in code-behind (= function with attribute [AllowStaticCommand]) to be able to use the functions RedirectToUrl/RedirectToLocalUrl/etc.

Tried to use
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
and
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery, endResponse: true);

But both lead to the following error:

DotVVM Debugger: Error 403:
request rejected: Pages can not be loaded using Javascript for security reasons.
Try refreshing the page to get rid of the error.
If you are the developer, you can disable this check by setting DotvvmConfiguration.Security.VerifySecFetchForPages.ExcludeRoute(“Test default”). [dest: empty, site: same-origin]

The only way to reload a page from a static command in code-behind is as I found out:
DotvvmRequestContext .GetCurrent(DotvvmMiddleware.ConvertHttpContext(HttpContextExtensions.GetOwinContext(HttpContext.Current))) .RedirectToLocalUrl(HttpContext.Current.Request.Url.PathAndQuery);

What I want to know now is:
Is there a better way to gain access to the Context?
Or are there better ways to reload a page from static command in code-behind?

Best regards

Hi!

From what I know, you have two options of accessing Context:

  1. The HttpContext provided by DotVVM (has some useful stuff):
    image

  2. The one provided by ASP.NET Core (has some other useful stuff):
    image

This latter one needs to be injected in your page/markupControl in a way similar to this:
image

2 Likes

The IDotvvmRequestContext is a registered scoped service, so you can request it via dependency injection. For instance, if the method is defined in a static command service, just add IDotvvmRequestContext parameter into the constructor and make sure it’s registered as transient or scoped (it can’t be a singleton). If you don’t want that, you can inject the context into the dothtml file using a @service context = DotVVM.Framework.Hosting.IDotvvmRequestContext directive, and then passing context as an argument.

2 Likes

Now I understand the case at hand. My last response was not helpful because static commands need static methods in which Context is not readily available because it is not a static member of DotVVM.Framework.ViewModel.DotvvmViewModelBase.

1 Like

@exyi
Thanks for the two possible solutions! :ok_hand:

On first read, I had problems to understand what you meant with “transient” and “scoped” (- I’m new in using OWIN & Katana -), but after a wider search on the net I found the meanings of these terminologies. :grin:

PS: I think, adding your answer to the docs of static commands on the homepage, would make it a lot easier to find the right solutions next time and in addition for other users too. :wink:

@MarianV
Thanks anyway for your try to help! :wink:

Best regards

1 Like