views:

82

answers:

1

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.

+3  A: 

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

p.campbell
Each line represents a FAQ in my environment.Some FAQs can display images.These images can have different urls (if url protocol is http or https, if the image is displayed in development mode or in production mode).Thus, these FAQs are real data but with some url variations.
labilbe
then you should keep your FAQ entries better structured - image url protocol as one column, image name as other column, etc - then load all of this stuff into a template on accessing a specific FAQ entry
Axarydax
@labilbe: you might want to consider keeping the link information in your data and dynamically generating the `ActionLinks` in a loop.
CAbbott