asp.net MVC framework (Part II): URL path selection

Source: Internet
Author: User
Tags url example

A brief summary of the first part

In the first part of the series, we created an ecommerce web site that showed three types of URLs:

URL format Behavior URL Example
/products/categories Browse All Product Categories /products/categories
/products/list/category List the products in a category /products/list/beverages
/products/detail/productid Show details of a particular product /products/detail/34

We process these URLs by creating a ProductsController class like the following:

After adding the above class to our application, the ASP.net MVC framework automatically directs incoming URLs to the appropriate action method on our controller to handle the request.

In today's post, we'll delve into how this URL mapping happens, and explore the more advanced path selection (routing) scenarios that we can leverage in the ASP.net MVC framework. I will also demonstrate how you can easily select a scene for the unit test URL path.

What does the ASP.net MVC URL path selection system do?

The ASP.net MVC framework includes a flexible URL path selection system that allows you to define URL mapping rules in your application. The path selection system has 2 main purposes:

Mapping incoming URLs to applications and directing them so that the correct controller and action methods are executed to handle these requests

Build URLs that can be used to callback controllers/actions to the client (for example, form submission, <a href= "" > Links, and AJAX calls, etc.)

The ability to use URL mapping rules to handle both incoming and outgoing URL scenarios adds a lot of flexibility to the application code. This means that if we want to change the URL structure of the application in the future (for example, by renaming/products to/catalog), we can modify a set of mapping rules for the application hierarchy without altering any code in the controller or view template.

Default asp.net MVC URL path selection rule

By default, when you use Visual Studio to create a new project with the ASP.net MVC Web application template, it adds a ASP.net application class to the project. This is implemented in the Global.asax background code:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.