HttpContext in Razor Views

I tried porting some ASPX markup to Razor, but the compiler made a mistake.

ASPX (works great):

<% if (node.IsAccessibleToUser(Context)) { %>
    // markup
<% } %>

CSHTML (throws an error):

@if (node.IsAccessibleToUser(Context)) {
    // markup
}

Argument 1: cannot be converted from "System.Web.HttpContextBase" to "System.Web.HttpContext"

How to get a link to HttpContextin Razor mode? Is it correct to use HttpContext.Currentor do I need to check the visibility of the site map node in a different way?

+3
source share
3 answers

WebViewPage.Context is an instance of HttpContextBase. WebViewPage.Context.ApplicationInstance.Context is an instance of HttpContext.

@if (node.IsAccessibleToUser(Context.ApplicationInstance.Context)) {
    // markup
}
+11
source

@Martin, , Node ( ), :

public static class NodeExtensions
{
    public static bool IsAccessibleToUser(this Node node)
    {
        // access HttpContext through HttpContext.Current here 
    }
}

, :

@if(node.IsAccessibleToUser()) {}

, HttpContext .

+2

Yes, you can use HttpContext.Current. This will give you access to request and response data.

+1
source

All Articles