ASP.net MVC自定义路由处理程序/约束 [英] ASP.net MVC custom route handler/constraint

查看:77
本文介绍了ASP.net MVC自定义路由处理程序/约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个MVC网站搭配以下网址:

I need to implement an MVC site with urls per below:


  • 组别/产品/ 1 /维基

  • 组别/产品/ 2 /维基

  • 类别1 /子类别1 /产品/ 3 /维基

  • 类别1 /子类别1 /子类别3 /产品/ 4 /维基

  • 等。等等。

在这里的匹配标准是URL以结束维基。

where the matching criteria is that the url ends with "wiki".

不幸的是,下面包罗万象的只能在URL的最后一部分:

Unfortunately the below catch-all works only in the last part of the url:

routes.MapRoute("page1", // Route name
                "{*path}/wiki", // URL with parameters
                new { controller = "Wiki", action = "page", version = "" } // Parameter defaults

所以我想知道什么是实现这一可能的选择,我还没有时间去通过MVC可扩展性选项?任何样品/例子是真是太棒了!

I have not had the time to go through the MVC extensibility options so I was wondering what are the possible choices for implementing this? Any sample/example would be just fantastic!

推荐答案

正如你提到的,包罗万象的参数只能出现在路线的结尾 - 你已经发布将抛出一个运行在code时间误差,给你死亡的黄色画面,如果你连试运行应用程序。

As you mentioned, the catch-all parameter can only appear at the end of a route - the code that you have posted will throw a run-time error and give you the yellow screen of death if you even try to run your application.

有用于构建自定义路由方案的几个扩展点。这些是 - 路线,RouteBase和IRouteHandler

There are several extensibility points for building custom routing scenarios. These are - Route, RouteBase, and IRouteHandler.

您可以创建路线延长RouteBase来处理生成的列表。通常情况下,你会遵循有一个构造函数在资源(控制器名称),然后分配给它的路线是负责一个列表,然后在您的global.asax映射的格局。下面是一些例子code可以从建立:

You can create a generated list of routes to handle by extending RouteBase. Typically you would follow the pattern of having a constructor that takes in a resource (controller name), and then assigning it a list of routes it was responsible for, and then mapping it in your global.asax. Here is some example code you can build from:

public class MyRoute : RouteBase
{
    private List<Route> _routes = new List<Route>();

    public MyRoute(string resource)
    {
        // make a Resource property, not shown in this example
        this.Resource = resource;

        // Generate all your routes here
        _routes.Add(
            new Route("some/url/{param1}",
            new McvRouteHandler {
                Defaults = new RouteValueDictionary(new {
                    controller = resource,
                    action = actionName
                }),
            Constraints = new RouteValueDictionary()
        );
        _routes.Add(...); // another new route   
    }

    public override RouteData GetRouteData(HttpContextBase context)
    {
        foreach (var route in _routes)
        {
            var data = route.GetRouteData(context);
            if (data != null)
            { 
                return data;
            }
        }
        return null;
    }

    public override VirtualPathData GetVirtualPath(RequestContext context, RouteValueDictionary rvd)
    {
        foreach (var route in _routes)
        {
            var path = route.GetVirtualPath(context, rvd);
            if (path != null)
            { 
                return path;
            }
        }
        return null;
    }
}

要使用你的路由类,做一个 routes.Add(新MyRoute(第1页));在你的Global.asax

To use your routing class, do a routes.Add(new MyRoute("page1")); in your Global.asax.

如果您需要更多的控制,可以实现一个IRouteHandler,而不是创建MvcRouteHandlers()为你的路由如图所示在上面的例子中,使用你自己的IRouteHandler。这将允许您覆盖从请求数据选择控制器的逻辑。

If you need even more control, you can implement an IRouteHandler, and instead of creating MvcRouteHandlers() for your routes as shown in the above example, use your own IRouteHandler. This would allow you to override the logic of selecting the controller from the request data.

整个框架是非常可扩展的,但你需要学习相当多,以做正确。我建议只不过是重新安排,如果你可以在你的URL如果可能采取包罗万象的参数的优势。

The entire framework is extremely extensible, but you would need to learn quite a bit in order to do it properly. I would suggest simply rearranging your URL's if possible to take advantage of the catch-all parameter if you can.

这篇关于ASP.net MVC自定义路由处理程序/约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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