Creating a custom route constraint in asp.net mvc

Source: Internet
Author: User

Create a Routing constraint (C #)

You can use routing constraints to restrict browser requests that match a particular path. You can use a regular expression to create a routing constraint.

For example, suppose you have defined routes as follows:

Listing 1-global.asax.cs

Routes. Maproute (

"Product",

"Product/{productid}",

New {controller= "Product", action= "Details"}

);

Listing 1 contains a route named product. You can use this Product route to map browser requests to Productcontroller, as follows:

Listing 2-controllers\productcontroller.cs

Using SYSTEM.WEB.MVC;

Namespace Mvcapplication1.controllers

{

public class Productcontroller:controller

{

Public ActionResult Details (int productId)

{

return View ();

}

}

}

Note: the Details () action receives a single parameter named ProductID. This parameter is an integer parameter.

The route defined in Listing 1 will matches any one of the URLs:

?/product/23

?/PRODUCT/7

Unfortunately, this route also matches the following URLs:

?/product/blah

?/product/apple

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.