ASP.NET MVC 3:如何打开从地址确定控制器操作 [英] ASP.NET MVC 3 : How to turn determine Controller Action from Url

查看:109
本文介绍了ASP.NET MVC 3:如何打开从地址确定控制器操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

惊讶我没有在任何地方找到这个答案,我怎么能确定哪些控制器/动作将在MVC 3调用一个给定的URL?

Surprised I'm not finding this answer anywhere, how can I determine what Controller/Action will be invoked for a given URL in MVC 3?

我真正想知道的:
我怎么能确定是什么ControllerAction会被调用为MVC 3给定的网址是什么? ....啊

What I really want to know: "how can I determine what ControllerAction will be invoked for a given URL in MVC 3?" ....yeah

所以,无论是我不知道的神奇方法做到这一点的:

So, either I'm not aware of the magic method that does this:

ControllerActionInfo GetControllerActionInfo(字符串URL)

或者,我会创造它自己做什么MVC做时,它得到一个HTTP请求。​​

Or, I will have to create it myself doing whatever MVC does when it gets an http request.

我问到这个问题在计算器上的目的是,我可以节省一些时间反向工程这一行为。正确答案应该类似于:

My purpose of asking about this on StackOverflow is that I can save some time reverse engineering this behavior. The correct answer should resemble:

这里是你如何能做到这一点:和一些code将遵循

Here's how you can do it: and some code would follow.

推荐答案

您必须使用一个虚拟的HttpContext和Htt的prequest类如下:

You have to use a dummy HttpContext and HttpRequest classes as follows:

public class DummyHttpRequest : HttpRequestBase {

    private string mUrl;

    public DummyHttpRequest(string url) {
        mUrl = url;
    }

    public override string AppRelativeCurrentExecutionFilePath {
        get {
            return mUrl;
        }
    }

    public override string PathInfo {
        get {
            return string.Empty;
        }
    }

}

public class DummyHttpContext : HttpContextBase {

    private string mUrl;

    public DummyHttpContext(string url) {
        mUrl = url;
    }

    public override HttpRequestBase Request {
        get {
            return new DummyHttpRequest(mUrl);
        }
    }

}

编辑:同时,可以延长 DefaultControllerFactory ,并添加一个简单的方法来获取想要的信息,而不是一个实例控制器。 (注:的它只是一个样本,你要支持其他因素,如 ActionNameAttribute 等)

Also, you can extend the DefaultControllerFactory and add a simple method to get the desired information instead of an instance of Controller. (Note: It's merely a sample, you have to support other aspects like ActionNameAttribute and so on)

public class ControllerActionInfo {

    public ControllerActionInfo(Type controllerType, MethodInfo action) {
        ControllerType = controllerType;
        Action = action;
    }

    public Type ControllerType { get; private set; }
    public MethodInfo Action { get; private set; }

}

public class DefaultControllerFactoryEx : DefaultControllerFactory {

    public ControllerActionInfo GetInfo(RequestContext requestContext, string controllerName) {
        Type controllerType = GetControllerType(requestContext, controllerName);

        if (controllerType == null) {
            return null;
        }

        MethodInfo actionMethod = controllerType.GetMethod(requestContext.RouteData.GetRequiredString("action"), BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);

        return new ControllerActionInfo(controllerType, actionMethod);
    }

}

然后,使用以下code片断访问到控制器:

Then, use following code snippet to get access to the controller:

DummyHttpContext httpContext = new DummyHttpContext("~/home/index");
RouteData routeData = RouteTable.Routes.GetRouteData(httpContext);
// IController controller = new DefaultControllerFactory().CreateController(new RequestContext(httpContext, routeData), routeData.GetRequiredString("controller"));
DefaultControllerFactoryEx controllerFactory = new DefaultControllerFactoryEx();

var result = controllerFactory.GetInfo(new RequestContext(httpContext, routeData), routeData.GetRequiredString("controller"));

这篇关于ASP.NET MVC 3:如何打开从地址确定控制器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆