I have a .cs file that generates HTML. Within it, I would like to make this code a little more fool proof by using the WebPageExecutingBase.Href() method, which I have done in many cshtml files before (mainly with the use of the tilde ~).
However, the links are broken on the generated HTML if I include just the tilde:
string html = "<a href=\"~/SomeFolder/SomeFile.cshtml\">Link</a>"; //Generates a broken link.
But I can't seem to import the appropriate using directive for use with the Href() method in the environment of a .cs file alone.
I have tried:
using System.Web;
using System.Web.WebPages;
And then trying to use WebPageExecutingBase.Href(), but while the "WebPageExecutingBase" part shows up in Intellisense, the "Href()" does not (and, in fact, generates a server-side error demanding a second 'object' argument, but I have used this same method with only one argument multiple times before).
I have tried to look up info on it here: http://msdn.microsoft.com/en-us/library/system.web.webpages.webpageexecutingbase(v=vs.111).aspx But to no avail.
I thought I had this right, but now I'm not even sure if I can use this method here at all.
Is there any way to implement the Href() method (or the same functionality therein) within HTML generated in a string in a .cs C# file?
The Href method is not intended for use in classes. You should use VirtualPathUtility instead. You will need to include a using directive for System.Web then you would do this: