Handling a mix of server side code and html in a resource file?
I'm trying to convert an ASP.NET web application to use resource files. I haven't used resource files before, so I'm just toying around with them and was wondering if this is possible.
I have a message that returns from a search when no results are found, that prompts the user to return to the home page. A lot of these pages have methods to determine what is the proper page to send the user to, so there are many sections with markup similar to this:
Sorry, but we could not find an item matching your search criteria.
Please adjust your search criteria or
<a href="<%= SomeMethodToDetermineUri() %>">
return to (SomePage)</a>
.
So basically, some type of message, followed by a link or a list of links. Getting the message part works fine, it's the server side code t开发者_如何转开发o generate links thats the problem.
What is the best way to put that into a resource file? It is able to recognize the html link part just fine, but the server side code gets inserted as plain text.
Is the only way to break it into 2 resources? (Which seems messy)
<%= Resources.Master.NoSearchResultsFound %>
<a href="<%= SomeMethodToDetermineUri() %>">
<%= Resources.Master.NoSearchResultsFoundReturnLinkText %>
</a>.
Or is there a way to get the page to evaluate the server code?
If you have similar functionality on your site I would suggest using User Controls or Master Pages to handle what you are attempting to do. I believe Resources are more for use with files outside of your project.
You don't want to put code of any kind into a resource file. Your solution is the correct one, even if you feel it's messy.
The only other thing you could do is to encapsulate this in a user control. The user control would go fetch the resources and therefore hide the messiness from the page.
Bah, I forgot about my old friend String.Format
<%= String.Format(Resources.Master.NoSearchResultsFound, SomeMethodToDetermineUri() %>
Works perfectly fine.
Then the value in the resource file is just
Sorry, but we could not find an item matching your search criteria.
Please adjust your search criteria or
<a href="{0}">return to (SomePage)</a>
.
精彩评论