I have a function Static1Url in a .cs which does a job of pre-pending a URL string:
namespace Website2012
{
public static class GlobalSiteFunctions
{
/// <summary>Returns a formatted URL mapping to Static1 on a CDN.</summary>
/// <param name="url">The URL to be formatted.</param>
/// <returns>Formatted string.</returns>
public static string Static1Url(string url)
{
return string.Format("{0}{1}", ConfigurationManager.AppSettings["Static1CDNUrl"], url);
}
}
}
I’d like to be able to access this from an aspx page like so:
<p>This is the complete URL: <%= Static1Url("my-file-name.jpg") %></p>
At the moment, to be able to do this I must use the following code:
<p>This is the complete URL: <%= Website2012.GlobalSiteFunctions.Static1Url("my-file-name.jpg") %></p>
If I add an Import statement at the top of the page (<%@ Import Namespace="Website2012" %>) then I can now use the following:
<p>This is the complete URL: <%= GlobalSiteFunctions.Static1Url("my-file-name.jpg") %></p>
Call me lazy but I’d prefer an even simpler solution, preferably one that didn’t involve an Import at the top of the page but then didn’t mean I had to use a lengthy function call. Is this possible?
EDIT (For the benefit of Imran Balouch)
I have come up with this idea for handling multiple page types, is this good practice:
public class MyPage : System.Web.UI.Page
{
public static bool Test()
{
return Functions.Test();
}
}
public class MyUserControl : System.Web.UI.UserControl
{
public static bool Test()
{
return Functions.Test();
}
}
private class Functions
{
public static bool Test()
{
return true;
}
}
The solution for this that can come in my mind is override the Page Class and in that overridden Page Class, write down your function and inherit every page of yours from the derived class, this way you can use it without using the import statement.
than your page must be like:
and you can use it like: