asp.net mvc按名称查找具有区域的控制器 [英] asp.net mvc Find Controller By Name With Area

查看:76
本文介绍了asp.net mvc按名称查找具有区域的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是从名称和区域中找到一个控制器.如果我当前的httpContext与要找到的控制器在同一区域内,则我已经成功完成了此操作.但是,我无法打电话给ControllerFactory来考虑区域.这是我的代码:

My goal is to find a controller from it's name and area. I have successfully done this if my current httpContext is within the same Area as the to-be-found controller. However, I cannot get my call to the ControllerFactory to take Area into consideration. Here's my code:

public static ControllerBase GetControllerByName(this HtmlHelper htmlHelper, string controllerName)
    {
      IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
      IController controller = factory.CreateController(htmlHelper.ViewContext.RequestContext, controllerName);
      if (controller == null)
      {
        throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "The IControllerFactory '{0}' did not return a controller for the name '{1}'.", factory.GetType(), controllerName));
      }
      return (ControllerBase)controller;
    }

由于它使用RequestContext作为参数,因此我向其添加了"area"的路由值,但没有更改.我可以对requestContext进行一些处理以考虑面积吗?我需要覆盖控制器工厂吗?如果是,那么特别要处理区域差异吗?

Since it's taking a RequestContext as a parameter, I've added an route value of "area" to it but with no change. Is there something I can do with the requestContext to some how take area into consideration? Do I need to override the controller factory--and if so, what in particular handles Area distinction?

更新:

以下是我拥有的AreaRegistration的示例:

Here is an example of a AreaRegistration I have:

public class StoresAreaRegistration : AreaRegistration
  {
    public override string AreaName { get { return "Stores"; } }

    public override void RegisterArea(AreaRegistrationContext context)
    {
      context.MapRoute(
          AreaName,
          AreaName + "/{controller}/{action}/{id}",
          new { area = AreaName, controller = "Home", action = "Index", id = UrlParameter.Optional }
      );
    }
  }

推荐答案

用于定位控制器的区域和命名空间位于RequestContext的RouteData中. 默认情况下会根据您当前正在处理的请求对它们进行填充,如果您需要更改它们,则必须在调用CreateController之前进行更改.当找不到控制器时,您可能会遇到异常.您还必须考虑到这一点.

The Area and Namespaces used to locate a controller are in the RouteData of the RequestContext. They are populatd by default based off of the request you are currently serving up, if you need to change them you have to do so before calling CreateController. You may get an exception when a controller cannot be found so you'll have to account for that as well.

更新:请注意,您必须创建一个新的RequestContext.如果您重复使用现有的解决方案,则会破坏操作和解决方法;稍后在此请求中进行查看.

UPDATE: Note, you MUST create a new RequestContext. If you reuse the existing one it will mess with the resolution of actions & views later on down the line in this request.

var tempRequestContext = new RequestContext(Request.RequestContext.HttpContext, new RouteData());
tempRequestContext.RouteData.DataTokens["Area"] = "";
tempRequestContext.RouteData.DataTokens["Namespaces"] = "YourCompany.Controllers";
var controller = ControllerBuilder.Current.GetControllerFactory()
                .CreateController(tempRequestContext, "ControllerName");

if(controller != null)
{
    //TODO: Implement your logic here
}

这篇关于asp.net mvc按名称查找具有区域的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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