I am setting up a feature on my site that requires displaying content that is stored in the database. This content will frequently have links to other resources on the site.
Stored in the db:
"Lorem ipsum dolor sit <a href='http://mysite.com/somecontroller/someaction'>amet</a>."
For debugging and other reasons, I don’t want to hard code that url. I want to replace http://mysite.com/somecontroller/someaction with UrlHelper.Action("somecontroller", "someaction").
I could write a replacement based on my own made up convention. That would be easy and accomplish what I want to do. However, I thought I might be missing some cleaner or more standard solution. So, is there a cleaner more maintainable way to do this, or should I go ahead with a convention based replacement?
Replacement is probably best, and easiest to maintain. Another way to handle it is to allow ASP.Net to parse the code. You’d have to create your own VirtualPathProvider which would allow Views to be pulled from the database in addition to the file system. Then you could use
Which would render the string in the database as if it were a file. Although this isn’t very safe, and you would need to modify the content before the VirtualPathProvider rendered it. Here is a guide which explains how to implement your own VirtualPathProvider.