如何RouteBase.GetRouteData工作或指针执行模式匹配 [英] How does RouteBase.GetRouteData work or pointers for implementing pattern matching

查看:169
本文介绍了如何RouteBase.GetRouteData工作或指针执行模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看执行的定义特定URL模式我的HttpModule是忽略的一个选项。

I'm looking at implementing an option for defining specific URL patterns that my HttpModule is to ignore.

我希望能够定义过滤器,如:

I'm wanting to be able to define "filters" such as:

/Admin/{*}
/Products/{*}/Search

这应该过滤掉的网址,如:

Which should filter out urls like:

http://mysite.com/admin/options
http://mysite.com/products/toys/search

但不能滤除      http://mysite.com/orders      http://mysite.com/products/view/1

but not filter out http://mysite.com/orders http://mysite.com/products/view/1

大致相同如何ASP.NET MVC已经注册相匹配的模式路径。 我看着菲尔哈克的路线调试器的源$ C ​​$ C,认为它会告诉我的RouteBase.GetRouteData(..)是如何工作的,但它只是利用它。

Much the same as how ASP.NET MVC has registered routes which match a pattern. I've looked at the source code of Phil Haack's Route Debugger, thinking it might show me how the RouteBase.GetRouteData(..) works, however it just utilizes it.

我似乎无法找到显示此RouteBase.GetRouteData是如何工作的任何实例(或找到实际的源$ C ​​$ C的话)。

I cannot seem to find any examples that show how this RouteBase.GetRouteData actually works (or find the actual source code for it).

如果任何人都可以在正确的方向如何(或模式匹配)的正常开展,这将是巨大的指向我。

If anyone can point me in the right direction for how this (or pattern matching) is normally implemented that would be great.

PS:我已经知道我可以使用普通的前pressions,但希望有一个非常具体的规则集

P.S: I already know I can use Regular expressions, but would like to have a very specific rule set.

推荐答案

更新

既然你想编写一个HTTP模块是非常密切的模仿作品 System.Web.Routing 呢,那么也许你应该使用的 ILSpy ,反映了组装,看看它做什么?

Since you want to write an HttpModule that very closely mimics the work System.Web.Routing does, then perhaps you should use ILSpy and reflect the assembly and see what it does?

原来的答案(保留给后人)

目前还不清楚,如果你谈论的是ASP.NET MVC或关于Spring MVC或有关Spring.NET的扩展ASP.NET MVC。如果是第一或第三:

It's not clear if you're talking about ASP.NET MVC or about Spring MVC or about Spring.NET's extensions to ASP.NET MVC. If it's the first or the third:

有关你的第一个例子:

管理​​/ {*}

解决方案#1以下将解决这个问题。对于你的第二个例子:

solution #1 below will address it. For your second example:

产品/ {*} /搜索,解决方案#2将解决这个问题(如果有一个需要验证,或者确实有一些有效的那里)

Products/{*}/Search, solution #2 will address it (if there's a need to validate or to actually have something valid there)

这两个解决方案是:

  1. 创建路由:采取这些参数(但奈何他们,或不关心他们)
  2. 创建,检查这些参数的要求(从路线),然后替换它们究竟应该有一个ActionFilter。

在你的航线节中的的global.asax.cs

routes.MapRoute("AdminAnything",
    "Admin/{*anything}",
    new { controller = admin, action = "Show" }
    );

这将导致以下网址,以解决在管理控制器的显示操作(从而忽视了输入你的愿望):

This will cause the following URLs to resolve to the Show action in the Admin controller (thus ignoring the inputs as you desire):

Admin/Options  
Admin/Anything-I-Want-here

解决方案2

现在,第二个是棘手的,因为你确实有东西在里面的。

Solution 2

Now, the second one is trickier because you actually have something in between it.

例输入:

Product/{*}/Search  

您可以编写拦截请求,看着它,并替换成任何你想要的那个值一个ActionFilter。

You can write an ActionFilter that intercepts the request, looks at it, and replaces that value with whatever you want.

首先,必要的路线:

routes.MapRoute("ProductSearch",
    "Products/{searchType}/Search,
    new { controller = "Products", action = "Search" }
    );

那么你的控制器动作:

Then your controller action:

public ActionResult Search(string searchType)
{
    //do nothing with searchType here.
}

如果你想真正取代的东西,你可以发送一个隐藏的表单字段在查看和处理,在Actionfilter:

If you wanted to actually replace that with something, you could send in a hidden form field in your view and process that in the Actionfilter:

public class SearchValidationActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if(filterContext.ActionParameters.ContainsKey("searchType") && filterContext.HttpContext.Request.Headers["hiddenSearchType"].IsNotNullOrEmpty()))
        {
        var actualSearchType = filterContext.ActionParameters["hiddenSearchType"] as SearchType;
        var searchType = filterContext.ActionParameters["searchType"];
        if (actualSearchType != null)
        {
            if (searchType.IsNullOrEmpty())
        {
            filterContext.Result = new SearchRedirectResult(actualSearchType.Name);
        }
        else if (!actualSearchType.Name.Equals(searchType))
        {
            filterContext.Result = new SearchRedirectResult(actualSearchType.Name,);
        }
        }
    }
        base.OnActionExecuting(filterContext);
    }
}

基本上,溶液#2取入任何东西,以及基于所述隐藏搜索类型,即传递到实际的搜索类型

Basically, Solution #2 takes in anything, and based on the hidden search type, passes that to the actual search type.

这篇关于如何RouteBase.GetRouteData工作或指针执行模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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