MVC3路由基础

编程入门 行业动态 更新时间:2024-10-26 18:22:57
本文介绍了MVC3路由基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我学习MVC路由。希望我的问题不看傻了,请帮助:)

I am learning MVC routing. Hope my question doesn't look silly, and please help :)

public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults );

参考MSDN说,它需要一个字符串,字符串,对象,所以我尽量让一个小的变化(增加了一个我眼前的一切只是mod的名字,看看它的工作原理):

Msdn reference says it takes a String, String, Object, so I try to make a small change (added a "my" in front of everything just to mod the names and see if it works):

public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{myresource}.axd/{*mypathInfo}"); routes.MapRoute( "myDefault", // Route name "{mycontroller}/{myaction}/{myid}", // URL with parameters new { mycontroller = "Home", myaction = "Index", myid = UrlParameter.Optional } // Parameter defaults );

它不工作了。这是这些字符串在{}大括号和匿名对象值格式的格式。

It doesn't work any more. What is the format of these strings in "{}" curly brackets, and the anonymous object value formats.

{控制器} / {行动} / {ID}/产品/显示/饮料

{controller}/{action}/{id} /Products/show/beverages

{}表/Details.aspx/Products/Details.aspx

{table}/Details.aspx /Products/Details.aspx

博客/ {行动} / {}进入/博客/显示/ 123

blog/{action}/{entry} /blog/show/123

{}的ReportType / {每年} / {月} / {}天/销售/ 2008/1/5

{reporttype}/{year}/{month}/{day} /sales/2008/1/5

{区域设置} / {}行动/ US /节目

{locale}/{action} /US/show

{}语言 - {国家} / {}行动 / EN-US /节目

{language}-{country}/{action} /en-US/show

{控制器} / {行动} / {ID}的http://服务器/应用/产品/显示/饮料

{controller}/{action}/{id} server/application/Products/show/beverages

{}资源个.axd / {*} PATHINFO HTTP://server/application/WebResource.axd D = ...

{resource}.axd/{*pathInfo} server/application/WebResource.axd?d=...

我google'd左右,但所有的职位似乎认为我所知道的格式,并且找不到任何细节explanation.Do他们要动如{控制器} {行动} {ID}等,或名称他们将无法正常工作?是否默认匿名对象值名称需要匹配他们呢?此外,什么是*意味着{*} PATHINFO我无法找到的解释,neihter。谢谢你。

I google'd around, but all the posts seem to assume I know the formats, and couldn't find any detail explanation.Do they have to be fixed names like {controller} {action} {id} etc, or they won't work? Does the default anonymous object value names need to match them, too? Further, what does the "*" mean in {*pathInfo} I couldn't find the explanation for that, neihter. Thank you.

推荐答案

首先,我们需要在这里的一些定义。

First, we need some definitions here.

让我们打破缺省路由。

routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults );

在这种情况下,预设于第2行,仅仅是用于识别路线的文本名称

In this case, Default, on line 2, is merely a text name used to identify the route.

在3行是URL模式。这定义了路由如何匹配。在括号中的东西是占位符。它们映射到参数名称。所以{}控制器映射到控制器名称。 {}动作映射到操作名称,并{ID}映射到参数调用ID。

On line 3 is the url pattern. This defines how the route is matched. the things in braces are placeholders. They map to parameter names. so {controller} maps to the controller name. {action} maps to action name, and {id} maps to parameter called id.

在4号线是默认对象。当他们无法从URL推断出该对象提供默认值。

On line 4 is the defaults object. This object supplies default values when they cannot be inferred from the url.

因此​​,如果我们采取所有一起,这里有一些结论,我们可以得出:

So, if we take all that together, here are some conclusions we can draw:

当他们无法从URL字符串来推断的默认对象只提供值。因此,当传入请求仅仅是 / ,从第4行的dfault值用于控制器和动作。如果传入的请求是 /布拉赫,然后在第4行提供的默认控制器将被忽略,而不是MVC查找 BlahController 。然而,由于没有指定动作,使用从第4行的默认操作首页。

the default objects only supply values when they cannot be inferred from the url string. Thus, when the incoming request is just /, the dfault values from line 4 are used for controller and action. If the incoming request is /Blah, then the default controller supplied on line 4 is ignored, and instead MVC looks for BlahController. However, since no action was specified, the default action from line 4 is used Index.

这里要记住的关键一点是,在第4行的值,如果没有行中的URL匹配仅用3。

The key thing to remember here is that the values on line 4 are only used if nothing matches the url in line 3.

所以,当你改变了一切,你的循环把一切。它是一个无意义的路线,因为没有定义控制器或操作要使用的,并且在这两个值中,以完成路由所需的。因此,MVC无法弄清楚你想要什么控制器。或与此有关操作方法。

So, when you changed everything, you threw everything for a loop. It's a meaningless route, because nothing defines which controller or action to use, and those two values are required in order to complete the route. As such, MVC can't figure out what controller you want. Or action method for that matter.

另外一个例子:

routes.MapRoute( "Example", "Home/{action}/{myid}", new { controller = "NotHome", action = "Index", myid = UrlParameter.Optional } );

由于没有在URL没有 {控制器} 占位符,那么NotHome使用默认值,这使得MVC寻找 NotHomeController 。因此,的网址/主页/关于/ 3 将意味着控制器=NotHome,行动=关于,和身份识别码= 3。

Because there is no {controller} placeholder in the url, then the default of "NotHome" is used, which makes MVC look for NotHomeController. So a url of /Home/About/3 would mean that controller = "NotHome", action = "About", and myid = 3.

所有的一切,在归路,的东西的必须在控制器和行动在最低值填充。 ID是可选的,可以被重命名为任何你喜欢的。但是,事情必须设置控制器和动作参数或MVC不知道如何路由的事情。

All in all, in an incoming route, something must fill in the value for controller and action at a minimum. id is optional, and can be renamed to whatever you like. But, something must set controller and action parameters or MVC doesn't know how to route things.

此外,请记住,默认路由(或有效的默认路由)必须放在最后在列表中,否则没有其他途径将被匹配。

Also, remember, the default route (or an effective default route) must come last in the list, otherwise no other routes will be matched.

的 {* PATHINFO} 位被称为蛞蝓。它基本上是一个通配符说:这一切都点塞进一个名为PATHINFO参数后。因此,如果你有{}资源个.axd / {*} PATHINFO键,这样的网址: HTTP://blah/foo.axd /富/酒吧/巴兹/冰然后两个参数生成,一个叫资源,其中将包括富和一个名为 PATHINFO 包含富/酒吧/巴兹/冰。

The {*pathInfo} bit is called a slug. it's basically a wildcard saying "everything after this point is stuffed into a parameter called pathInfo". Thus if you have "{resource}.axd/{*pathInfo}" and a url like this: blah/foo.axd/foo/bar/baz/bing then two parameters get created, one called resource, which would contain foo and one called pathInfo which contains foo/bar/baz/bing.

更多推荐

MVC3路由基础

本文发布于:2023-11-12 13:45:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   基础

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!