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

**URL:** <https://forum.dotvvm.com/t/static-commands-how-to-gain-access-to-property-context-in-code-behind-view-models/252>\
**Category:** Discussion in English\
**Created:** [September 4, 2024, 8:07am UTC](https://forum.dotvvm.com/t/static-commands-how-to-gain-access-to-property-context-in-code-behind-view-models/252 "2024-09-04T08:07:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Twoleeland](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/twoleeland/32/381_2.png) [@Twoleeland](https://forum.dotvvm.com/u/Twoleeland)\
**Post date:** [September 4, 2024, 8:07am UTC](https://forum.dotvvm.com/t/static-commands-how-to-gain-access-to-property-context-in-code-behind-view-models/252/1 "2024-09-04T08:07:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [September 5, 2024, 11:43am UTC](https://forum.dotvvm.com/t/static-commands-how-to-gain-access-to-property-context-in-code-behind-view-models/252/2 "2024-09-05T11:43:44Z")

</div>

Hi!

> [@Twoleeland](#):
>
> Is there a better way to gain access to the `Context`?

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

1. The HttpContext provided by DotVVM (has some useful stuff):  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/fae9b657643ab6d42196ad07250f4e3c18ce41ea.png)

2. The one provided by [ASP.NET](http://ASP.NET) Core (has some other useful stuff):  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/08f52e14df90a297472adaf554d5028ae0fe168c.png)

This latter one needs to be injected in your page/markupControl in a way similar to this:  
 ![image](https://forum.dotvvm.com/uploads/default/original/1X/14398f6ad8c1b5e6635d37bbd4ce3b5d2db05b17.png)

---

<div class="post-metadata">

**Author:** ![exyi](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/exyi/32/9_2.png) [@exyi](https://forum.dotvvm.com/u/exyi)\
**Post date:** [September 7, 2024, 6:18pm UTC](https://forum.dotvvm.com/t/static-commands-how-to-gain-access-to-property-context-in-code-behind-view-models/252/3 "2024-09-07T18:18:01Z")

</div>

> [@Twoleeland](#):
>
> Is there a better way to gain access to the `Context`?

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.

---

<div class="post-metadata">

**Author:** ![MarianV](https://forum.dotvvm.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MarianV](https://forum.dotvvm.com/u/MarianV)\
**Post date:** [September 8, 2024, 11:27am UTC](https://forum.dotvvm.com/t/static-commands-how-to-gain-access-to-property-context-in-code-behind-view-models/252/4 "2024-09-08T11:27:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Twoleeland](https://forum.dotvvm.com/user_avatar/forum.dotvvm.com/twoleeland/32/381_2.png) [@Twoleeland](https://forum.dotvvm.com/u/Twoleeland)\
**Post date:** [September 9, 2024, 1:09pm UTC](https://forum.dotvvm.com/t/static-commands-how-to-gain-access-to-property-context-in-code-behind-view-models/252/5 "2024-09-09T13:09:09Z")

</div>

@exyi  
Thanks for the two possible solutions! 👌

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

@MarianV  
Thanks anyway for your try to help! 😉

Best regards
