基于请求参数MVC 3使用不同的控制器 [英] MVC 3 use different controllers based on request parameters

查看:177
本文介绍了基于请求参数MVC 3使用不同的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经冲刷为这个过去几天的解决方案的网络和我没有找到太多。但愿我没有使用正确的术语,它是一件容易的事。

I've been scouring the web for a solution for this the past couple days and I'm not finding much. Hopefully I'm not using the right terminology and it's an easy thing to do.

我想用像路径:

/{projectId}

和有一个地方的地方早在生命周期中,我有机会获得我可以查询数据库或会话对象来获取控制器名称用于此请求的路径值字典。然后,可以指定控制器使用 route.Values​​ [控制器] = controllerName; ,并请求被通过控制器的请求参数所有的爵士乐和制作等

And have a place somewhere early on in the life-cycle where I have access to the route value dictionary that I can query a database or a session object to get the controller name to use for this request. Then be able to specify the controller to use route.Values["controller"] = controllerName; and have the request be made through that controller with all the jazz of request parameters and the like.

这可能吗?

我目前使用的领域,有这样的路径:

I'm currently using areas, and have paths like:

/ProjectType1/{projectId}
/ProjectType2/{projectId}

但我发现它真正的头痛领域中的所有 Html.Link 的处理与恨定义每个项目类型的新领域。我很想找到的东西更有活力。

but I'm finding it a real headache dealing with areas in all the Html.Link's and hate defining new areas for each project type. I would love to find something more dynamic.

推荐答案

您可以编写一个定制路线:

You could write a custom route:

public class MyRoute : Route
{
    public MyRoute(string url, IRouteHandler routeHandler)
        : base(url, routeHandler)
    { }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var rd = base.GetRouteData(httpContext);
        var projectId = rd.GetRequiredString("projectId");

        // TODO: Given the projectId decide which controller to choose:
        // you could query the database or whatever it is needed here

        if (projectId == "1")
        {
            rd.Values["controller"] = "Foo";
        }
        else if (id == "2")
        {
            rd.Values["controller"] = "Bar";
        }
        else if (id == "3")
        {
            rd.Values["controller"] = "Baz";
        }
        else
        {
            // unknown project id
            throw new HttpException(404, "Not found");
        }

        // we will always be using the Index action but this could
        // also be variable based on the projectId
        rd.Values["action"] = "index";

        return rd;
    }
}

,然后注册吧:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.Add(new MyRoute("{projectId}", new MvcRouteHandler()));
}

现在


  • / 1 将调用 FooController的/索引

  • / 2 将调用 BarController /索引

  • / 3 将调用 BazCont​​roller /索引

  • / something_else 将抛出404未找​​到

  • /1 will invoke FooController/Index
  • /2 will invoke BarController/Index
  • /3 will invoke BazController/Index
  • /something_else will throw 404 Not found

这篇关于基于请求参数MVC 3使用不同的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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