详解ASP.NET MVC 路由 (上)
Posted DotNet
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了详解ASP.NET MVC 路由 (上)相关的知识,希望对你有一定的参考价值。
来源:Alan_beijing
cnblogs.com/wangjiming/p/7040843.html
例子引入
先看看如下例子,你能完全明白吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MVCDemo
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes();//特性路由
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");//忽略资源文件
routes.MapRoute(
name: "Default",//路由名,唯一
url: "{controller}/{action}/{id}",//路由URL
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
//路由URL默认值
namespaces: new[] { "MVCDemo.Controllers" },//命名空间避免二义性
constraints: new { id = @"^\d*$" }//约束
);
}
}
}
注:该例子没有加入区域
一、URI、URL与URN
1.1 URI、URL和URN定义
URI(Uniform Resource Identifier)代表统一资源标识符,标识资源的字符串;
URN(Uniform Resources Name)代表统一资源名称,互联网上资源的名称;
1.2 URI、URL和URN三者之间的关系图
1.3 对URI、URL和URN三者之间解析
本解析基于1.2 URI、URL和URN三者之间的关系图。
从数学关系:URI=URL+URN+URL∩URN;很容易看出,URL一定是URI,但URI不一定是URL,同理,URN一定是URI,但URI不一定是URN;
1.4 URL应具有特点
域名便于记忆和拼写;
简短;
便于输入;
可以反映出站点结构;
应该是“可破解的”,用户可以通过移除URL的末尾,进而到达更高层次的信息体系结构;
持久、不能改变
1.5 小结
通常情况下,URI代表同意资源标识符(Uniform Resource Identifier)。URI是标识了一个资源的字符串。从技术角度看,所有URL都是URI。
W3C认为“URL是一个非正式的概念,但它非常有用:URL是URI的一种类型,它通过表示自身的主要访问机制来标识资源”,换句话说,URI是某种资源的标识符,而URL则为获取该资源提供了具体的信息。
注释:资源是一个抽象概念,既可以指一个文件,也可以指方法调用的结果或服务器上的一些其他内容。
二、路由概述(传统路由)
2.1 WebForm URL与Route URL
(1)WebForm中,对URL的传入请求通常映射到磁盘上的物理文件,如.aspx文件。例如对http://server/application/Product.aspx?id=4的请求映射到名为Products.aspx文件,该文件包含代码和标记用于呈现对浏览器的响应,一般请求示过程抽象如下:
(2)ASP.NET MVC中,通常地,URL映射并非映射到具体磁盘上的物理文件,这是基于ASP.NET路由特性,主要有两个过程,即URL映射和URL生成(接下来讲解)。在ASP.NET路由中,您可以定义URL模式,该模式包含在处理URL请求时使用的值的占位符。在运行时,运用程序名称后面的URL部分根据您所定义的URL模式分析为离散值。
例如,在请求http://server/application/Products/show/beverages时,路由分析器可以将值Products、show和beverages传递给请求的处理程序。相反,在一个不由URL路由管理的请求中,/Products/show/beverages片段将被解释为运用程序中的一个文件的路径。
2.2 ASP.NET 路由与 URL 重写
ASP.NET 路由不同于其他 URL 重写方案。URL 重写通过在将请求发送到网页之前实际更改 URL 来处理传入请求。例如,一个使用 URL 重写的应用程序可能会将 URL 从 /Products/Widgets/ 更改为 /Products.aspx?id=4。此外,URL 重写通常没有相应的 API 来创建基于模式的 URL。在 URL 重写中,如果更改了 URL 模式,则必须手动更新包含原始 URL 的所有超链接。由于 ASP.NET 路由可以从 URL 提取值,所以处理传入请求时不更改 URL。如果必须创建一个 URL,则将参数值传递到为您生成 URL 的方法中。若要更改 URL 模式,请在某位置更改该模式,您在应用程序中创建的基于该模式的所有链接将自动使用新模式。
2.3 路由定义
A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. A handler can also be a class that processes the request, such as a controller in an MVC application. To define a route, you create an instance of the Route class by specifying the URL pattern, the handler, and optionally a name for the route.
译文:路由是一种被映射到某个处理程序的URL模式。处理程序可能是一个物理文件,如在WebForms运用程序中的aspx文件。处理程序也可能是一个处理请求的类,如MVC应用程序中的控制器。要定义路由,您可以通过指定URL模式、处理程序和路径的名称来创建路由类的实例。
You add the route to the application by adding the Route object to the static Routes property of the RouteTable class. The Routes property is a RouteCollection object that stores all the routes for the application.You typically do not have to write code to add routes in an MVC application. Visual Studio project templates for MVC include preconfigured URL routes. These are defined in the MvcApplication class, which is defined in the Global.asax file.
译文:你可以通过将路由对象添加到RouteTable类的静态路由属性中的方式将路由添加到应用程序中。路由属性是一个为应用程序存储所有路由的路由对象。在MVC应用程序中,您通常不需要编写代码来添加路由。VS项目模板为MVC包含了预先配置的URL路由。这些都是在MvcApplication类中定义的,被定义在Global.asac 文件中。
2.4 URL Patterns(URL模式)
A URL pattern can contain literal values and variable placeholders (referred to as URL parameters). The literals and placeholders are located in segments of the URL which are delimited by the slash (/) character.
译文:URL模式可能包含文字值和变量占位符(称为URL参数)。文字和占位符位于URL的片段中,由斜杠(/)字符分隔。
When a request is made, the URL is parsed into segments and placeholders, and the variable values are provided to the request handler. This process is similar to the way the data in query strings is parsed and passed to the request handler. In both cases variable information is included in the URL and passed to the handler in the form of key-value pairs. For query strings both the keys and the values are in the URL. For routes, the keys are the placeholder names defined in the URL pattern, and only the values are in the URL.
译文:当发出请求时,URL被解析为片段和占位符,且变量值被提供给请求处理程序。这个过程类似于查询字符串中的数据被解析并传递给请求处理程序的方式。在这两种情况下,变量信息都包含在URL中,并以键值对的形式传递给处理程序。对于查询字符串,键和值都在URL中。对于路由,键是URL模式中定义的占位符名称,在URL中仅仅是值。
In a URL pattern, you define placeholders by enclosing them in braces ( { and } ). You can define more than one placeholder in a segment, but they must be separated by a literal value. For example, {language}-{country}/{action} is a valid route pattern. However, {language}{country}/{action} is not a valid pattern, because there is no literal value or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the language placeholder from the value for the country placeholder.
译文:在URL模式中,通过将它们封装在括号(以及)中来定义占位符。您可以在一个段中定义多个占位符,但是必须用文字值分隔它们。例如,语言-国家/行动是一种有效的路线模式。然而,语言国家/action不是一个有效的模式,因为占位符之间没有文字值或分隔符。因此,路由不能决定将语言占位符的值与国家占位符的值分隔开。
The following table shows valid route patterns and examples of URL requests that match the patterns.
下表显示了有效的路由模式和与模式匹配的URL请求示例。
Typical URL Patterns in MVC Applications
译文:MVC运用程序中的经典模式
URL patterns for routes in MVC applications typically include {controller} and {action} placeholders.
译文:在MVC运用程序中,路由URL模式通常包含控制器和动作占位符。
When a request is received, it is routed to the UrlRoutingModule object and then to the MvcHandler HTTP handler. The MvcHandler HTTP handler determines which controller to invoke by adding the suffix "Controller" to the controller value in the URL to determine the type name of the controller that will handle the request. The action value in the URL determines which action method to call.
译文:当接收到一个请求时,它被路由到UrlRoutingModule对象,然后发送到MvcHandler HTTP处理程序。MvcHandler HTTP处理程序通过向URL中的控制器值添加后缀“控制器”来确定要调用哪个控制器,以确定控制器的类型名称,该控制器将处理请求。URL中的操作值决定调用哪个操作方法。
For example, a URL that includes the URL path /Products is mapped to a controller named ProductsController. The value in the action parameter is the name of the action method that is called. A URL that includes the URL path /Products/show would result in a call to the Showmethod of the ProductsController class.
译文:例如,一个包含URL路径 /产品 的URL映射到一个名为ProductsController的控制器。action参数中的值是调用的操作方法的名称。一个包含URL路径 /产品/show 的URL会导致对ProductsController类的Showmethod的调用。
The following table shows the default URL patterns, and it shows examples of URL requests that are handled by the default routes.
译文:下表显示了默认的URL模式,它展示了由默认路由处理的URL请求的示例。
The route with the pattern {resource}.axd/{*pathInfo} is included to prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.
For IIS 7.0, no file-name extension is needed. For IIS 6.0, you must add the .mvc file-name extension to the URL pattern, as in the following example:
译文:带有模式资源的路由axd/pathInfo,被用于防止Web资源文件的请求,例如WebResource,axd或ScriptResource传递到控制器。对于IIS 7.0,不需要任何文件名称扩展。对于IIS 6.0,您必须添加.mvc文件扩展名到URL模式中,如下面的例子:
如在VS2013使用MVC模板创建项目时,自动生成类RouteConfig.
2.5 Adding Routes to a Web Forms Application(添加路由到WebForm运用程序)
In a Web Forms application, you create routes by using the MapPageRoute(String, String, String) method of the RouteCollection class. The MapPageRoute method creates a Route object and adds it to the RouteCollection object. You specify properties for the Route object in parameters that you pass to the MapPageRoute method.
译文:在Web Forms应用程序中,您可以使用路由选择类的MapPageRoute(字符串、字符串、字符串)方法创建路由。MapPageRoute方法创建一个路由对象并将其添加到RouteCollection对象。您可以在传递给MapPageRoute方法的参数中指定路由对象的属性。
Typically, you add routes in a method that is called from the handler for the Application_Start event in the Global.asax file. This approach makes sure that the routes are available when the application starts. It also enables you to call the method directly when you unit-test the application. If you want to call a method directly when you unit-test the application, the method that registers the routes must be static (Shared in Visual Basic) and must have a RouteCollection parameter.
译文:通常地,在全局Global.asax文件中,您可以在一个叫做Application_Start 方法里添加路由。该方法确保当应用程序启动时,路由是可以使用的。它还使您可以在对应用程序进行单元测试时直接调用该方法。如果您想在对应用程序进行单元测试时直接调用方法,那么注册路由的方法必须是静态的(在Visual Basic中是共享的),并且必须具有一个路由参数。
The following example shows code from a Global.asax file that adds a Route object that defines two URL parameters named action and categoryName. URLs that have the specified pattern are directed to the physical page named Categories.aspx.
译文:下面的示例展示了来自.Globalasax文件的代码,该代码添加了一个路由对象,该对象定义了两个名为action和类别名称的URL参数。具有指定模式的url被定向到名为分类.aspx的物理页面。
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categoriespage.aspx");
}
2.6 Adding Routes to an MVC Application
If you adopt the MVC convention of implementing controllers by creating classes that derive from the ControllerBase class and giving them names that end with "Controller", you do not need to manually add routes in an MVC application. The preconfigured routes will invoke the action methods that you implement in the controller classes.
译文:如果您通过创建从控制器基类派生的类来实现控制器的MVC约定,并给它们以“控制器”结尾的名称,那么您就不需要在MVC应用程序中手动添加路由了。预配置的路由将调用您在控制器类中实现的操作方法。
If you want to add custom routes in an MVC application, you use the MapRoute(RouteCollection, String, String) method instead of the MapPageRoute(String, String, String) method.
译文:如果您想在MVC应用程序中添加自定义路由,您可以使用MapRoute(RouteCollection、String、String)方法,而不是MapPageRoute(字符串、字符串、字符串)方法。
The following example shows the code that creates default MVC routes in the Global.asax file, as defined in the Visual Studio project template for MVC applications.
译文:下面的例子展示了在全局中创建默认MVC路由的代码。asax文件,正如在Visual Studio项目模板中定义的MVC应用程序。
public class MvcApplication : System.Web.HttpApplication
{
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 = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
2.7 Setting Default Values for URL Parameters(为URL参数设置默认值)
When you define a route, you can assign a default value for a parameter. The default value is used if a value for that parameter is not included in the URL. You set default values for a route by assigning a dictionary object to the Defaults property of the Route class. The following example shows how to add a route that has default values, by using the MapPageRoute(String, String, String, Boolean, RouteValueDictionary) method.
译文:当您定义一个路由时,您可以为一个参数指定一个默认值。如果该参数的值没有包含在URL中,则使用默认值。通过将dictionary对象分配给route类的默认属性,可以为路由设置默认值。
下面的例子展示了如何通过使用MapPageRoute(字符串、字符串、字符串、布尔值、RouteValueDictionary)方法添加具有默认值的路由。
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categoriespage.aspx",
true,
new RouteValueDictionary
{{"categoryName", "food"}, {"action", "show"}});
}
When ASP.NET routing handles a URL request, the route definition shown in the example (with default values of food for categoryName and showfor action) produces the results that are listed in the following table.
For MVC applications, overloads of the RouteCollectionExtensions.MapRoute method, such as MapRoute(RouteCollection, String, String, Object, Object), enable you to specify defaults.
2.8 Handling a Variable Number of Segments in a URL Pattern(在URL模式中处理可变数量的段)
Sometimes you have to handle URL requests that contain a variable number of URL segments. When you define a route, you can specify that if a URL has more segments than there are in the pattern, the extra segments are considered to be part of the last segment. To handle additional segments in this manner you mark the last parameter with an asterisk (*). This is referred to as a catch-all parameter. A route with a catch-all parameter will also match URLs that do not contain any values for the last parameter. The following example shows a route pattern that matches an unknown number of segments.
译文:有时您必须处理包含一个可变数量的URL段的URL请求,其中。当您定义一个路由时,您可以指定,如果一个URL的片段比模式中有更多的段,那么额外的段被认为是最后一部分的一部分。要以这种方式处理额外的段,您可以用星号(*)标记最后一个参数。这被称为一个笼统的参数。一个包含所有参数的路由也将匹配不包含最后一个参数的任何值的url。下面的示例展示了一个匹配未知数量的段的路由模式。
/ query / { queryname } { * queryvalues }
When ASP.NET routing handles a URL request, the route definition shown in the example produces the results that are listed in the following table.
译文:当ASP.NET路由处理一个URL请求时,示例中所示的路由定义产生了下表中列出的结果。
2.9 Adding Constraints to Routes(为路由添加约束)
In addition to matching a URL request to a route definition by the number of parameters in the URL, you can specify that values in the parameters meet certain constraints. If a URL contains values that are outside the constraints for a route, that route is not used to handle the request. You add constraints to make sure that the URL parameters contain values that will work in your application.
译文:除了通过URL中参数的数量来匹配URL请求外,还可以指定参数中的值满足特定的约束条件。如果一个URL包含的值超出了路由的约束,那么该路由就不会被用于处理请求。您添加了一些约束,以确保URL参数包含在您的应用程序中工作的值。
Constraints are defined by using regular expressions or by using objects that implement the IRouteConstraint interface. When you add the route definition to the Routes collection, you add constraints by creating a RouteValueDictionary object that contains the verification test. The key in the dictionary identifies the parameter that the constraint applies to. The value in the dictionary can be either a string that represents a regular expression or an object that implements the IRouteConstraint interface.
译文:约束是通过使用正则表达式或使用实现IRouteConstraint接口的对象来定义的。当将路由定义添加到路由集合时,通过创建一个包含验证测试的RouteValueDictionary对象来添加约束。字典中的键标识约束应用到的参数。字典中的值可以是表示正则表达式的字符串,也可以是实现IRouteConstraint接口的对象。
If you provide a string, routing treats the string as a regular expression and checks whether the parameter value is valid by calling the IsMatch method of the Regex class. The regular expression is always treated as case-insensitive. For more information, see .NET Framework Regular Expressions.
译文:如果您提供一个字符串,那么路由将字符串作为一个正则表达式来处理,并通过调用Regex类的IsMatch方法检查参数值是否有效。正则表达式总是被视为不区分大小写的。要了解更多信息,请参阅。净框架正则表达式。
If you provide an IRouteConstraint object, ASP.NET routing checks whether the parameter value is valid by calling the Match method of the IRouteConstraint object. The Match method returns a Boolean value that indicates whether the parameter value is valid.
译文:如果你提供一个IRouteConstraint对象,ASP.NET 路由通过调用IRouteConstraint对象的匹配方法, 以此来检查参数值是否有效。Match方法返回一个布尔值,该值表示参数值是否有效。
The following example shows how to use the MapPageRoute method to create a route that has constraints that limit what values can be included in the locale and year parameters. (In an MVC application, you would use the MapRoute method.)
译文:下面的示例展示了如何使用MapPageRoute方法创建一条路径,该路由限制了地区和年参数中可以包含的值。(在MVC应用程序中,您将使用map路由方法。)
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categoriespage.aspx",
true,
new RouteValueDictionary
{{"categoryName", "food"}, {"action", "show"}},
new RouteValueDictionary
{{"locale", "[a-z]{2}-[a-z]{2}"},{"year", @"\d{4}"}}
);
}
When routing handles a URL request, the route definition shown in the previous example produces the results that are listed in the following table.
2.10 Scenarios When Routing Is Not Applied(不应用路由时的场景)
Under some circumstances, ASP.NET routing does not handle a request even when is enabled for the Web site. This section describes some scenarios in which routing does not handle the request.
译文:在某些情况下,即使在Web站点启用时,ASP.NET 路由也不处理请求。本部分描述了一些场景,其中路由不处理请求。
A Physical File is Found that Matches the URL Pattern(找到与URL模式匹配的物理文件)
By default, routing does not handle requests that map to an existing physical file on the Web server. For example, a request for http://server/application/Products/Beverages/Coffee.aspx is not handled by routing if a physical file exists at Products/Beverages/Coffee.aspx. Routing does not handle the request even if it matches a defined pattern, such as {controller}/{action}/{id}.
译文:默认情况下,路由不处理映射到Web服务器上现有物理文件的请求。例如,如果存在 Products/Beverages/Coffee.aspx.物理文件,请求http://server/application/Products/Beverages/Coffee.aspx 不被路由处理。即使它与已定义的模式相匹配,路由不处理请求,例如控制器/动作/id。
If you want routing to handle all requests, even requests that point to files, you can override the default behavior by setting the RouteExistingFiles property of the RouteCollection object to true. When you set this value to true, all requests that match a defined pattern are handled by routing.
译文:如果您想要路由处理所有请求,甚至指向文件的请求,您可以通过设置RouteCollection对象的属性RouteExistingFiles为true, 以此来覆盖默认值。当您将这个值设置为true时,匹配定义模式的所有请求都由路由处理。
Routing Is Explicitly Disabled for a URL Pattern(为URL模式显式禁用路由)
You can also specify that routing should not handle certain URL requests. You prevent routing from handling certain requests by defining a route and specifying that the StopRoutingHandler class should be used to handle that pattern. When a request is handled by a StopRoutingHandler object, the StopRoutingHandler object blocks any additional processing of the request as a route. Instead, the request is processed as an ASP.NET page, Web service, or other ASP.NET endpoint. You can use the RouteCollection.Ignore method
译文:您还可以指定路由不应该处理某些URL请求。通过定义一条路由,并指定StopRoutingHandler类应该用于处理该模式,从而避免了处理某些请求的路由。当一个StopRoutingHandler对象处理请求时,StopRoutingHandler对象会阻塞请求作为路由的任何附加处理。相反,请求是作为ASP.NET 页面来处理的,网络页面,Web服务,或其他ASP.NET端点。您可以使用 RouteCollection.Ignore方法。
public static void RegisterRoutes(RouteCollection routes)
{
routes.Ignore("{resource}.axd/{*pathInfo}");
}
2.11 How URLs Are Matched to Routes(url如何与路由匹配)
When routing handles URL requests, it tries to match the URL of the request to a route. Matching a URL request to a route depends on all the following conditions:
译文:当路由处理URL请求时,它尝试将请求的URL与路由匹配。将URL请求匹配到路由取决于以下条件:
The route patterns that you have defined or the default route patterns, if any, that are included in your project type.
译文:您已经定义的路由模式或默认的路由模式,如果有的话,它们包含在您的项目类型中。
The order in which you added them to the Routes collection.
译文:您将它们添加到路由集合的顺序。
Any default values that you have provided for a route.
译文:您为某个路由提供的任何默认值。
Any constraints that you have provided for a route.
译文:您为路线所提供的任何约束。
Whether you have defined routing to handle requests that match a physical file.
译文:是否定义了路由来处理与物理文件匹配的请求。
For example, suppose that you add routes with the following patterns:
译文:例如,假设您添加了以下模式:
Route 1 is set to {controller}/{action}/{id}
译文: 路径1设置为{controller}/{action}/{id}
Route 2 is set to products/show/{id}
译文:路线2设置为 products/show/{id}
Route 2 will never handle a request because Route 1 is evaluated first, and it will always match requests that could also work for Route 2. A request for http://server/application/products/show/bikes seems to match Route 2 more closely, but it is handled by Route 1 with the following values:
controller is products.
action is show.
id is bikes.
译文:路由2永远不会处理请求,因为路由1首先被匹配,它总是匹配可能在2号路径上工作的请求。请求http://server/application/products/show/bikes似乎比赛路线2更紧密,但它是由路线1以下值:
控制器products.
行动是show.
id是bikes.
Default values are used if a parameter is missing from the request. Therefore, they can cause a route to match a request that you did not expect. For example, suppose that you add routes with the following patterns:
Route 1: {report}/{year}/{month}, with default values for year and month.
Route 2: {report}/{year}, with a default value for year.
Route 2 will never handle a request. Route 1 might be intended for a monthly report, and Route 2 might be intended for an annual report. However, the default values in Route 1 mean that it will match any request that could also work for Route 2.
译文:如果请求中缺少一个参数,则使用默认值。因此,它们可以导致一条匹配您没有预料到的请求的路由。例如,假设您添加了以下模式:
路线1:报告/年/月,年和月默认值。
路线2:报告/年,年默认值。
路由2永远不会处理请求。第1条可能是针对每月报告的,而路由2可能是针对年度报告的。但是,路由1中的默认值意味着它将匹配任何可能用于路由2的请求。
You can avoid ambiguity in the patterns by including constants, such as annual/{report}/{year} and monthly/{report}/{year}/{month}.
If a URL does not match any Route object that is defined in the RouteTable collection, ASP.NET routing does not process the request. Instead, processing is passed to an ASP.NET page, Web service, or other ASP.NET endpoint.
译文:您可以通过包含常量来避免模式中的歧义,例如 annual/{report}/{year} and monthly/{report}/{year}/{month}。
如果URL不匹配在RouteTable集合中定义的任何路由对象,ASP.NET路由不处理请求。相反,处理被传递给一个ASP.NET Page ,Web服务,或其他ASP.NET端点。
2.12 路由二义性
只在同一个解决方案中,存在两个以及以上相同控制器下的相同action,当URL请求时,会出现二义性。
二义性Demo目录结构
RouteConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MVCDemo
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
/Controllers/RouteDemo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCDemo.Controllers
{
public class RouteDemoController : Controller
{
// GET: /RouteDemo/
public ActionResult Index()
{
return View();
}
}
}
/Controllers/NewFolder1/RouteDemo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCDemo.Controllers.NewFolder1
{
public class RouteDemoController : Controller
{
//
// GET: /RouteDemo/
public ActionResult Index()
{
return Content("路由二义性测试");
}
}
}
测试结果
看完本文有收获?请转发分享给更多人
关注「DotNet」,提升.Net技能
以上是关于详解ASP.NET MVC 路由 (上)的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET MVC5 新特性:Attribute路由使用详解