设置ASP.Net MVC 4路由自​​定义变量细分 [英] Setting up ASP.Net MVC 4 Routing with custom segment variables

查看:74
本文介绍了设置ASP.Net MVC 4路由自​​定义变量细分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始与一对夫妇领域的应用工作(基本网格主/细节类型的系统。)我期待​​到MVC(特别是4)取的很好的路由功能的优势,我只是不得到它我presume。

I just began working on an application with a couple areas (basic grid master / details type system..) I'm looking into taking advantage of the nice routing features in MVC (4 specifically) and I'm "just not getting it" I presume.

目前定义的唯一途径是基本一:

Currently the only route defined is the basic one:

routes.MapRoute("Default", 
            "{controller}/{action}/{id}", 
            new { controller = "Account", action = "Index", id = UrlParameter.Optional }
        );

这是很好的,它与我们所定义的范畴,所以我假设它必须知道用户,并在位置/地区用户在基于语境适当的控制器路线..到目前为止,不错..

which is fine, it works with the areas we have defined, so I'm assuming it must know where the user is and route to the appropriate controller based contextually on the location / area that the user is in.. so far nice..

现在,我试图建立能够处理一条新的路径。

Now, i'm trying to set up a new route that can handle

/someController/someAction/{statusName} 

和具体是这样的:

/OrderManager/List/New 

/OrderManager/List/Viewed

,其中新,是新的地位,并有行动签名的样子:

where "New" is the "New" status and have the Action Signature look like:

public ActionResult List(string statusName)

我是假设我可以只添加下面的默认的标识statusName,而不是Id的新途径,当然,怎么轰将路由机制认识的区别是:

i was assuming I could just add the new route below the default one identifying "statusName" instead of Id, but of course, how the H would the routing mechanism know the difference between :

/控制器1 /动作1/15

/controller1/action1/15

/控制器2 /动作2 /新

/controller2/action2/new

我没有尝试的形式,加入了静态的路线

I did try adding a "static" route in the form of

routes.MapRoute("Default", 
            "ControllerName/ControllerAction/{statusName}", 
            new { statusName = UrlParameter.Optional }
        );

我以为可以劫持只是一条路线,做一些特别的东西吧,但是要知道无济于事,路由器停止在第一场比赛?我假设是无论如何解决这个问题的错误的方式。

I thought I could "hiJack" just that one route and do something special with it, but to know avail, the router stops at first match?? I'm assuming that was the wrong way to address this issue anyhow..

所以现在我经历越来越为类似的想法:

so now I'm going through the idea of getting to something like:

/somecustomroutename/somesortValue

恩。
    / OrderManagerList / viewNew

ex. /OrderManagerList/viewNew

而这些路线将基本上是别名。我原以为加入以下路线将做的伎俩:

where these routes would basically be "aliases". I had thought that adding the following route would do the trick:

 routes.MapRoute("Default_List",
          "OrderManagerList/{statusName}",
          new {controller="OrderManager", action="List", statusName= UrlParameter.Optional }
      );

在的OrderManager控制器上的相关操作:

with the associated action on the OrderManager controller:

public ActionResult List(string statusName)

不管我怎么努力,该参数为空,或者资源无法找到

no matter what I try, the argument is null, or the "resource cannot be found"

我知道控制器需要有相应的视图文件..但是这不是这里的问题,这个问题是我了解路由的尝试。

I know the controllers need to have a corresponding View file.. but that's not the issue here, the issue is my attempt at understanding the routing..

所以我的问题..重要的是,我错过什么关于MVC(4)路由?即使是像我这样的傻瓜一些好文章,明白了吗?

SO my questions.. fundamentally, what am I missing about the routing in MVC (4)? even some good articles for a simpleton like myself to understand?

我的理解;定义路由,并绘制它的端点不过,我觉得我不理解的是,该机正在..

my understanding; define a route, and map it's "endpoint".. however, i think i'm not understanding the assumptions that the machine is making..

总之,让我知道,如果进一步解释/需要编辑..

anyhow, let me know if further explain / edit is required..

先谢谢了。

推荐答案

路由的基本原理是,他们是从上往下进行评估和路由系统使用了第一场比赛,而不是最佳匹配。

The basic principle of routes is that they are evaluated from the top down, and the routing systems uses the first match, not the best match.

这个含义是,你必须订购你的路由特异性的顺序排列,最具体的路线第一个和最后最普遍的途径。

The implication of this is that you must order your routes in order of specificity, with the most specific route first and the most general route last.

通过这个原理之后,让我们看看你的情况。最初,你只有缺省路由定义的:

With this principle in mind, let’s look at your situation. Initially, you have only the default route defined:

routes.MapRoute("Default", 
        "{controller}/{action}/{id}", 
        new { controller = "Account", action = "Index", id = UrlParameter.Optional }
    );

URL模式为{控制器} / {行动} / {ID}。就其本身而言,这将匹配任何三段的URL,而不会匹配只有不到三段的URL。然而,第三个输入参数是默认的参数,它定义默认值,第一和第二段以及表示该第三区段是可选的。这种下一个效果是使具有0,1,2或3个片段的路由匹配的URL。

The URL pattern is "{controller}/{action}/{id}". By itself, this would match any three-segment URL, and would not match any URL that had less than three segments. However, the third input parameter is the default parameter, which defines defaults for the first and second segment and indicates that the third segment is optional. The next effect of this is to make the route match URL having 0,1, 2 or 3 segments.

现在您要添加其中第三URL段被映射到一个statusName参数的路由,并能处理像网址:

Now you want to add a route where the third URL segment is mapped to a "statusName" parameter, and can handle URLs like:

OrderManager/List/New 
OrderManager/List/Viewed 

有您可以在这里两种基本方法。您可以1)创建将只处理这两个网址,或2)你可以尝试建立以处理一般情况下更广泛的途径非常具体的路线。让我们来看看第一种情况下第一。你可以创建一个路由如下:

There are two basic approaches you can take here. You can 1) create a very specific route that will handle these two URLs only, or 2) you can try and create a more general route to handle the general case. Let’s look at the first case first. You can create a route as follows:

routes.MapRoute("", "OrderManager/List/{statusName}",
              new { Controller = "OrderManager", Action = "List" });

请注意,由于这条线路比默认路由更具体的,你必须把之前的默认路由这条路线的

Note that because this route is more specific than the default route, you must put this route before the default route.

如果你想有一个更普遍的路线,你需要决定如何路由将从默认路由不同,因为它们都将匹配有三段的URL。比方说,你决定,新航线将接受任何只包含在第三段字母,留下的缺省路由的处理包含任何数字。您可以使用路由约束做到这一点。举例如下,你可以写一个路线:

If you want to have a more general route, you need to decide how this route will differ from the default route, since they both will match URLs having three segments. Let’s say you decide that the new route will accept anything that only contains letters in the third segment, leaving the default route to handle anything containing numbers. You can do this using route constraints. For example you could write a route as follows:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            constraints: new { id = @"^\d+$" });

        routes.MapRoute(
            name: "NewRoute",
            url: "{controller}/{action}/{statusName}",
            defaults: new { Controller = "OrderManager", Action = "List" },
            constraints: new { statusName = "^[A-Za-z]+$" });

使用这两个路线,具有在第三区段仅字母任何三段的URL将会把第三区段到名为statusName可变的,而任何URL与第三区段的整数会将第三区段成变量,名为身份证。

With these two routes, any three-segment URL having only letters in the third segment will put the third segment into a variable called "statusName", whereas any URL with an integer in the third segment will put the third segment into a variable called "id".

在实际复​​杂的应用程序,路线可能很复杂,这是编写单元测试你的路线,以确保当您添加或修改路线,你不要搞砸非常有利的。

In any applications of real complexity, routes can get complicated, and it is very advantageous to write unit tests for your routes, to insure that you don’t screw things up when you add or modify a route.

有关路由很好的参考,请参见斯科特·桑德森的书或看到的MSDN文档

For good references on routing, see Scott Sanderson's book or see the MSDN documentation

这篇关于设置ASP.Net MVC 4路由自​​定义变量细分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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