Controller Path in ASP.NET-MVC

给你一囗甜甜゛ 提交于 2019-12-12 20:17:52

问题


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:


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



回答2:


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?




回答3:


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>") 



回答4:


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.



来源:https://stackoverflow.com/questions/773798/controller-path-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!