Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net C# ResolveClientUrl inside Class

I have the following code:

public class NavigationPath
{
    private string menuItems = "<li>" +
                                    "<a href=\"#\">home</a>" +
                               "</li>";

But I would like to have:

public class NavigationPath
{
    private string menuItems = "<li>" +
                                    "<a href=\"" + ResolveClientUrl("~/home.aspx") + "\">re</a>" +
                               "</li>";

However, I am not able to use ResolveClientUrl inside a Class. Any ideas?

like image 477
Marco Avatar asked Feb 10 '10 16:02

Marco


People also ask

What is ASP.NET C?

ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites. It allows you to use a full featured programming language such as C# or VB.NET to build web applications easily.

Is ASP.NET like C#?

ASP.NET is a web application development framework used to develop web applications using different back-end programming languages like C# where C# is used as an object-oriented programming language to develop web applications along with ASP.NET.

Can I use .NET with C?

. NET Framework is an object oriented programming framework meant to be used with languages that it provides bindings for. Since C is not an object oriented language it wouldn't make sense to use it with the framework.

What is .NET ASP.NET and C#?

Basically, ASP.NET is a web delivery mechanism that runs either C# or VB.NET in the background. C# is a programming language that runs ASP.NET as well as Winforms, WPF, and Silverlight.


2 Answers

Instead of calling ResolveClientUrl on the Page object (or any controls), you can also use VirtualPathUtility.ToAbsolute("~/home.aspx"); which will give you the same result as calling ResolveUrl("~/home.aspx");

like image 149
Paulus E Kurniawan Avatar answered Sep 28 '22 23:09

Paulus E Kurniawan


ResolveClientUrl is a member of the System.Web.UI.Control class, hence it's accessible directly as:

var url = ResolveClientUrl("~/Some/Url/");

when called within the code of your asp.net page.

To use it inside a class you're going to have to pass the Page (or a control on the page) into the class in its constructor. Even then I'm not sure you'd be able to use it in the way you've indicated. You'd probably have to do something similar to:

public class NavigationPath
{
  private string menuItems = string.Empty;

  public NavigationPath(Page page)
  {
    menuItems = "<li>" + "<a href=\"" + page.ResolveClientUrl("~/Home.aspx") + "\">home</a>" + "</li>";
  }
}

And then inside your asp.net page do something like:

var navPath = new NavigationPage(this);
like image 28
Rob Avatar answered Sep 28 '22 23:09

Rob