I'm intending to create an extension for MVC that looks like this:
public static class DivExtension
{
public static MvcHtmlElement BeginDiv(this HtmlHelper helper)
{
return new MvcDiv(helper.ViewContext).BeginDiv("","");
}
public static MvcHtmlElement BeginDiv(this HtmlHelper helper, string id)
{
return new MvcDiv(helper.ViewContext).BeginDiv(id,"");
}
}
In Razor I could use it like this:
@{using(Html.BeginDiv())
{
<text>
This should appear inside a div <input type="text" value="oi" />
</text>
}
}
Which would generate the following HTML output:
<div>
This should appear inside a div <input type="text" value="oi" />
</div>
But imagine that instead of just creating a div, my code would receive a string representing roles and, if the logged in user does not belong to the specified Roles, the HTML within this extension should be suppressed:
public static class HtmlAuthSuppressor
{
public static MvcHtmlElement AuthHTML(this HtmlHelper helper, string roles)
{
//some code that would allow suppression
}
}
and If I use like this:
<b>Do you see something below?</b>
@{using(Html.AuthHTML("Role_Super_User"))
{
<text>
Congratz!!! You can see this, u are super, indeed. <input type="text" value="oi" />
</text>
}
}
The final HTML output, in case the user does not belong to the specified role, would be:
<b>Do you see something below?</b>
Is that possible?
UPDATE: I know I could generate an HTML like this:
<b>Do you see something below?</b>
<!--
Congratz!!! You can see this, u are super, indeed. <input type="text" value="oi" />
--!>
But that would reveal something to the client that I wouldn't like to reveal, besides making the response unnecessarily heavier.
You don't need to build an extension for that.. just do this:
<b>Do you see something below?</b>
@if (Request.IsAuthenticated && User.IsInRole("Role_Super_User"))
{
<text>
Congratz!!! You can see this, u are super, indeed. <input type="text" value="oi" />
</text>
}