Add custom routes for ASP. net mvc applications

Source: Internet
Author: User

Here, we will learn how to add custom routes to the asp.net mvc application. Use custom route entries to modify the default route table.

For some simple asp.net mvc applications, the default route table is enough. However, when you need to create a special route, you need to create a custom route.

Assume that a blog project is under development and you want to process the user's request address as follows:

/Archive/12-25-2009

When you enter this URL, you want to return the blog entry on the date of 12-. To achieve this routing, you need to add a custom route.

 

In this asp.net mvc application, this method contains a custom route called blog, which can accept URLs like/Archive/entydate.

Here we need to place the custom route on the default route. If it is reversed, we will always request the default route table.

The custom route will match the url starting with archive, and the url below will be processed by it.

/Archive/12-25-2009

/Archive/10-6-2004

/Archive/apple

The url of the custom route ing request is ArchiveController and the Entry is executed) action. When the entry method is called, entrydate is passed as a parameter of this method.

The ArchiveController code is as follows:

 

We can see that the entry method accepts a datetime type parameter. The mvc framework is pretty good enough to automatically convert entrydate to the datetime type. If entrydate in the url cannot be converted to datetime, an error is returned.

The above is the custom Routing Method for The asp.net mvc application.

  1. Learning how to use ASP. net mvc Routing
  2. Analysis of URL Rewrite in ASP. NET
  3. Introduction to ASP. net mvc Framework
  4. Introduction to MvcAjaxPanel in ASP. NET MVC
  5. ASP. net mvc Framework to save UpdatePanel

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.