c# - 是否可以在 ASP.NET MVC3 中检索某个 Url 字符串的 Controller 实例?

标签 c# .net asp.net-mvc asp.net-mvc-3 routes

如果我有一个像 /foo/bar/pewpew 这样的路由 .. 是否有可能获得该路由映射的 Controller 实例?

最佳答案

要获取 Controller 名称,您可以调用创建一个伪造的 HttpContextBase,它在其 Request 中返回您的 URL,然后将其传递给 RouteTable.Routes.GetRouteData 并检查 areacontroller 值。

要获取 Controller 实例,将由 HttpContextBaseRouteData 组成的 RequestContext 传递给 ControllerBuilder.Current.GetControllerFactory.CreateController

关于c# - 是否可以在 ASP.NET MVC3 中检索某个 Url 字符串的 Controller 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6982306/

相关文章:

c# - SerialPort.GetPortNames 多次返回同一个端口

c# - 如何使用 DotNumerics 解决线性规划问题?

asp.net-mvc - 有关网站安全的建议

c# - 如何在WPF中绑定(bind)UpdateSourceTrigger?

c# - 从其他 .dll 加载服务并隔离运行它们

mysql - 查询返回 "The method or operation is not implemented."错误

asp.net - 如何在不使用角色的情况下使用 ASP.NET Web API 实现基于声明的授权?

c# - 在 OData 响应 C# 中显示 @odata.id

C#.net WinForms 如何在文本框处于焦点时使文本框中的文本消失?

c# - 如何在自定义基于策略的授权中访问 dbcontext & session