Question

I have content like this stored in a database

<p>This a sample text. <%= Html.ActionLink("test", "myaction", "mycontroller") %></p>

The content is part of my data repository, that is the reason I want to keep it inside the database. I would like to know how it is possible to render it and execute it at compile time.

I am using it on an asp.net mvc project.

Thank you.

Was it helpful?

Solution

You can use VirtualPathProvider starting from framework 2.0, I believe.

You can build a new class that the ASP.Net runtime will query for just about every request. It will be important to keep this class tight, and if you say you'll be pulling from a database, I would definitely recommend implementing your system to load those files locally onto the web server, and not hit the database for every request. In addition, when you pull that file from the database, the runtime may decide to compile it... and you also dont want that to happen for every request.

But to answer your original question, its a VirtualPathProvider that you're looking for. You can also use this to pull from assembly resources if so desired.

OTHER TIPS

It sounds like you have a line of markup and source code stored as a string in a location in a table in your database?

Have you considered moving that data/code/values to a web.config instead?

Consider storing your environment config settings in web.config. i.e.

 <appSettings>
   <add Name="IsProduction" value="true" />
   <add Name="RequiresSecure" value="true" />

Your controller and model can read these values, and pass the environment settings along to the view.

When you're writing out those FAQ entries, you can modify the output with a simple if.

<% if (Model.IsProduction) //have your ViewModel pass along whether you're in Production mode, Dev mode, URLs to have SSL, or whatever criteria you like, etc.
{%>
    <!-- my production markup, with image URL, SSL'd etc. -->
    <img src="https://mysite.com/img.png" />
<%}
else  {%>

    <!-- my other Dev markup, with image URL, etc. -->
    <img src="https://myDevServer/img.png" />
<%} %>

It would typically be considered a bad practice, or even a dub-tee-eff, to keep code in your database. Consider the rule of 'keep data in your database'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top