views:

1289

answers:

5

How do I get the path of a controller? For example, I can get the path of a HtmlHelper like this:

 private static string GetVirtualPath(HtmlHelper htmlhelper)
 {
  string virtualPath = null;
  TemplateControl tc = htmlhelper.ViewDataContainer as TemplateControl;

  if (tc != null)
  {
   virtualPath = tc.AppRelativeVirtualPath;
  }

  return virtualPath;
 }
+1  A: 

The method you have there will return the relative aspx filename for a View - that's not really the location of an HtmlHelper.

When you say you want the path of a Controller, what do you actually mean? Your Controller is a class compiled in an assembly somewhere. Do you want to get the location of the source .cs file for the Controller? Or something different?

Steve Willcock
+3  A: 

Edit: The following will give the path to the assembly with the controller and the type name of the class with the controller action. Maybe a combination of these will give you what you're after, Aaron?

string assemblyPath = Assembly.GetExecutingAssembly().CodeBase;
string typeName = this.GetType().FullName;

They yield, for example, something like

file:///C:/Projects/TestApp/TestApp.UI/bin/TestApp.UI.DLL
TestApp.UI.Controllers.TestController

Provided that you place and name the controllers in the 'standard' ASP.NET MVC ways, a certain combination of the above might give you the correct full path to the C# file:

C:/Projects/TestApp/TestApp.UI/Controllers/TestController.cs

or the relative path:

Controllers/TestController.cs


The following will give the route to the controller action:

1) string path = Request.Url.AbsolutePath

2) string appPath = Request.ApplicationPath;
   string absPath = Request.Url.AbsolutePath;
   string path = appPath.Length <= 1 ? 
       absPath : absPath.Replace(appPath, "");

Example for the request for a TestController's Index action (http://localhost:50027/Test/Index): The above returns

1) /Test/Index
2) /Test/Index

For a website with base url at http://localhost:50027/blog, example for the request for a TestController's Index action (http://localhost:50027/blog/Test/Index): The above returns

1) /blog/Test/Index
2) /Test/Index
Ole Lynge
A: 

you can get the name of the controller like this

Url.RequestContext.RouteData.Values["controller"]

after getting the controllername you can resolve the path using the method

ResolveClientUrl("~/<ControllerName>")
Rony
you shouldn't build URLs like that manually... put it through Routing using RouteTable.Routes if you have to.
anurse
A: 

Steve Willcock- Yes, I want the location of the source file. Not the routing path, I want the actual path to the file.

Aaron

A: 

Aaron, it sounds like you're writing a quine, heh, but I presume this is for some unit test scaffolding or something? Maybe you really want to use a build script for some of what you're doing? You could put some commands in the Project's properties page that execute some processes on your controller directory upon build? Sorry if this is not at all what you're trying to do.

David