Website routing configuration in MVC Mode

Source: Internet
Author: User

The requests in Asp.net are based on physical files (. aspx). Therefore, a basic request corresponds to a specific file in the application.

Therefore, in Asp.net, there is no special management requirement for user requests. You only need to manage the physical files of the application.

 

In MVC, all these fundamental changes occur. User requests have nothing to do with physical files, and because of this feature, you can organize them at will.

The URL requested by the user. Unordered route settings can lead to disorder in your program. Therefore, in MVC, URL management is necessary.

 

The following are my summary methods.

1. A hierarchy is required.

For building an enterprise website, it is generally divided into the front-end and back-end. The front-end interface is used for anonymous users or registered users, and then the website administrator performs website data.

Operation interface. Generally, the frontend is in the form of http://www.site.com/xxxx. Background: http://www.site.com/admin/xxxx.

XXXX indicates the specific access content.

The foreground settings are as follows:

Home: http://www.site.com/or http://www.site.com/index

Company Profile: http://www.site.com/about

Company: http://www.site.com/news

Products: http://www.site.com/product

We: http://www.site.com/contat

......

 

Background

Login: http://www.site.com/admin/login

Product maintenance: http://www.site.com/admin/product

Http://www.site.com/admin/news

 

2. Reasonably match the url and controller

The Controller of MVC is located based on the Controller name. Therefore, the name and action of the Controller should not conflict with the access path defined in the front.

We use a website as an example. Because the structure is divided into foreground and background, we define the Controller as the frontend and background. Home is used at the front end and manage is used at the backend.

Home actions include Index, News, Product, ShowNews, ShowProduct (), About, and Contract.

Manage actions include Login, Product, ProductDetail, and AddProduct.

3. Routetable

Front-end

Routes. MapRoute (

"Home", // Route name

"{Action}/{Id}", // by default, the root site is sent to Home

New {controller = "Home", action = "Index", id = UrlParameter. Optional} // set the hosts page.

);

 

Background

Routes. MapRoute (

"Admin", // Route name

"Admin/{action}/{Id}", // hand over admin processing to Manager by default

New {controller = "Manage", action = "Index", id = UrlParameter. Optional} // set the hosts page.

);

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.