ASP. net mvc rest api application instance

Source: Internet
Author: User

Problems to be Solved

 

1: create a more concise/Product/Details/1 URL

 

Valid route definitions

Examples of matching URL

{Controller}/{action}/{id} /Products/Details/1
{Controller}/{action}/{id} /Products/1
{Controller}/{action}/{id}. {Format} /Products/Show/1.xml

 

"/Products/Details/1" is the route for viewing the Details method of the object by default in MVC development. What needs to be implemented is a more concise url representation, such as "/Products/1 ", returns the same result as "/Products/Details/1. This process is simple. You only need to add a new routing rule to the Global. asax file, but remember to add it before the default route.

 

2: Create an API for/Product/Show/1. xml

To implement the xml access API, you must first create an abstract class to process the routes of returned results and the serialization of xml results. Here the class library of MVC Contrib is used, you can go to MVC Contrib Code Plex to download and add references. See return new XMLResult in the code.

The Code is as follows:

Finally, you need to add the corresponding return type to the corresponding Action. Here is Show. See

The actual effect is as follows:

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.