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?
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.
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.
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.
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.