HTML5技术

【ASP.NET MVC 牛刀小试】 ASP.NET MVC 路由 - Alan_beijing(3)

字号+ 作者:H5之家 来源:H5之家 2017-06-29 11:01 我要评论( )

译文:例如,一个包含URL路径 /产品 的URL映射到一个名为ProductsController的控制器。action参数中的值是调用的操作方法的名称。一个包含URL路径 /产品/show 的URL会导致对ProductsController类的Showmethod的调用

译文:例如,一个包含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应用程序中手动添加路由了。预配置的路由将调用您在控制器类中实现的操作方法。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Asp.Net WebForm生命周期的详解 - 天使不哭

    Asp.Net WebForm生命周期的详解 - 天使不哭

    2017-06-15 09:00

  • 关于ASP.NET WebForm与ASP.NET MVC的比较 - 天使不哭

    关于ASP.NET WebForm与ASP.NET MVC的比较 - 天使不哭

    2017-06-09 09:02

  • [asp.net mvc 奇淫巧技] 04 - 你真的会用Action的模型绑定吗? - Emrys5

    [asp.net mvc 奇淫巧技] 04 - 你真的会用Action的模型绑定吗? - Emr

    2017-06-02 13:00

  • Amazing ASP.NET Core 2.0 - Savorboard

    Amazing ASP.NET Core 2.0 - Savorboard

    2017-05-25 14:00

网友点评
s