路由-MVC-ASP.NET [英] Routing-MVC-ASP.NET
问题描述
在大多数文章中,他们都放置了这段代码并对其进行了解释,但我觉得我没明白.任何人都可以用简单的术语来解释它.
In most of the articles, they put this code and explain it but I feel I am not getting it. could any body expalain it in simple terms please.
这个问题看起来很简单,但我无法正确理解.
This question is looks simple but I cannot get it correct in my head.
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
);
我的问题:
我们为什么使用route.IgnoreRoute以及为什么{}中的参数?
Why do we use route.IgnoreRoute and why the parameters in {} ?
Maproute具有第一个参数-默认值,类似的内容,第二个参数-"{controller}/{action}/{id}",这是什么和第三个参数,我们使用new吗?
Maproute has First parameter-Default, What that resembles, Second Parameter-"{controller}/{action}/{id}", What this for and third parameter, we use new ?
如何解释这些路由?
为什么所有这些?
到目前为止,我已经使用过Webforms,但是无法获取?
I have used webforms so far and Cannot get it in?
MVC中的任何一位Gurus都可以解释所有这些内容吗?
Any Gurus in MVC could explain all these please?
推荐答案
为什么要使用route.IgnoreRoute
Why do we use route.IgnoreRoute
这告诉路由忽略所有与提供的模式匹配的请求.在这种情况下,请忽略对axd
资源的任何请求.
This tells routing to ignore any requests that match the provided pattern. In this case to ignore any requests to axd
resources.
为什么使用{}中的参数?
and why the parameters in {} ?
{}
表示定界字符串是变量.在忽略路由中,使用此路由是为了匹配任何.axd请求.
The {}
indicates that the delimited string is a variable. In the ignore route this is used so that any .axd requests are matched.
Maproute具有第一个参数-默认值",类似于"
Maproute has First parameter-Default, What that resembles,
第一个参数是路由名称.当通过名称引用路由时,可以使用此方法.可能是我倾向于使用的null
.
The first parameter is the route name. This can be used when referring to routes by name. It can be null
which is what I tend to use.
第二个参数-"{controller}/{action}/{id}",这是什么
Second Parameter-"{controller}/{action}/{id}", What this for
这是匹配的模式.在这种情况下,它将设置默认路由,该默认路由是由控制器名称,操作名称和可选ID组成的url. URL http://mysite.com/Foo/Bar
将在Foo
控制器上调用Bar
方法.将url更改为http://mysite.com/Foo/Bar/1
会传递一个带有标识符id
和值1
的参数.
This is the pattern that is matched. In this case it is setting up the default route which is a url formed by the controller name, action name and an optional id. The url http://mysite.com/Foo/Bar
would call the Bar
method on the Foo
controller. Changing the url to http://mysite.com/Foo/Bar/1
would pass a parameter with the identifier id
and value 1
.
和第三个参数
and third parameter,
第三个参数提供默认值.对于默认路由,默认控制器名称为Home
,默认操作为Index
.结果是,对http://mysite.com
的请求将调用Home
控制器上的Index
方法.路由的id
部分被指定为可选.
The third parameter supplies defaults. In the case of the default route the default controller name is Home
and the default action is Index
. The outcome of this is that a request to http://mysite.com
would call the Index
method on the Home
controller. The id
part of the route is specified as being optional.
我们使用新的吗?
we use new ?
new
关键字正在使用.Net框架版本3中引入的对象初始化程序语法创建对象. Microsoft文章.
The new
keyword is creating an object using the object initializer syntax that was introduced in version 3 of the .Net framework. Microsoft article.
使用路由的主要优点是它为您的网址创建了约定.如果您创建了一个名为Account
的新控制器以及名为Index
和Review
的操作方法,则这些方法将分别在/Account
和Account/Review
处可用.
The major advantage of using routing is that it creates a convention for your urls. If you created a new controller called Account
and action methods called Index
and Review
then the methods would be availble at /Account
and Account/Review
respectively.
这篇关于路由-MVC-ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!