Use ASP. NET Web Api to build a REST-based service practice series of tutorials [upload] & mdash; & mdas

Source: Internet
Author: User

Series Navigation Address http://www.cnblogs.com/fzrain/p/3490137.html

Digress: This technical point is newly learned and does not belong to the original series. However, with the help of the Project Background of the original series, the external series is named, some new technologies may be added to this series in the future.

In Web Api 2.0, A New Route configuration method is proposed-Attribute-based Routing ), the configuration Routing method we introduced earlier is called Convention-based Routing. The new Routing configuration method is also applied in MVC5, therefore, this article will introduce the feature-based routing.

In the previous article, we handled such a business-implementing the Course Selection for students. We have customized a route data in "WebApiConfig". This route enables you to select a course and query all the student information selected for this course based on the course Id, I think the design is okay. In practical applications, query is generally used most often. Using Attribute Routing to register a route is more flexible, easier to control, and better compliant with Rest. This document describes the information of all students who select the course and the information of a student based on the Course name and Student name.

As the name suggests, new route will use a feature to implement route registration, as shown below:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple=, Inherited=   RouteAttribute(   Name { ;       Order { ;    Template { ;  

This class contains three attributes: Name indicates the Name of the route, Order indicates the route Order, and Template indicates the Template to match the URL.

I have not introduced much about the principles (I have not studied much). I can apply the technologies I have learned to the reality, and I am confused when I come up with a lot of principles. In the future, I need to study the Principles again.

Add a new method GetStudentsInfo in "EnrollmentsController:

[Route( IEnumerable<StudentBaseModel> GetStudentsInfo( courseName,  studentName=<Student>=  TheRepository.GetAllCourses().Where(c => c.Name == (course== = TheRepository.GetEnrolledStudentsInCourse(course.Id).OrderBy(s => (!= query.Where(s => s.FirstName == totalCount = results ==>

RouteAttribute is used on our Action. Analyze the URL template (). {courseName} will match the courseName parameter of Action. Another parameter of Action, studentName, is an optional parameter, that is to say, if the request does not provide a value, it is the default null string. If it is worthwhile, it will be assigned a value. So we add it after "{studentName? Mark as an optional URI parameter.

OK, that's simple. test once:

There is an error, but in any case the solution is always there. First, let's take a look at the cause of the error: There is a null reference in the method of the LearningControllerSelector class, so we have to look at this method:

  controllers = GetControllerMapping();               routeData = controllerName = routeData.Values[ (controllers.TryGetValue(controllerName,  version =  versionedControllerName = .Concat(controllerName,  (controllers.TryGetValue(versionedControllerName,  

After reading this code, we can easily find that this method is the method of the base class that we have rewritten to achieve current version control (details can be moved: http://www.cnblogs.com/fzrain/p/3558765.html ), in our rewriting method, we use the name of the Controller contained in RouteData, which is obtained by matching the routing and URI based on conventions. Therefore, we certainly do not have the name here. Because our project has a mix of 2-minute route configurations, and the custom method is for convention-based Routing configuration, we use the default option for Attribute Routing:

Add a judgment before "controllerName = routeData. Values:

 (   

The following result is returned when you request again:

Attribute Routing is a new custom Routing rule method, which is more flexible than traditional configuration methods, but scattered at the same time. In web APIs, the two-minute route configuration method can coexist. Therefore, this article only introduces a new configuration method. You can also select one option when creating a project.

This article Reference Links: http://www.cnblogs.com/aehyok/p/3449851.html

Http://www.cnblogs.com/artech/p/attribute-routing-01.html

Source Code address: https://github.com/fzrain/WebApi.eLearning

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.