(This article also published in my public number "dotnet daily Essence article", Welcome to the right QR code to pay attention to. )
Preface: Because of the combination of MVC and Web API technology stacks in ASP. NET 5, it is also possible to develop hybrid controllers.
As we all know, in the era of ASP. NET MVC 5 and Web API 2, the technology stack is independent (the development team is also independent). Although both can then be merged into the Owin, the controller code cannot be written together (because the base class is inconsistent). Admittedly, the MVC controller can also achieve the effect of a Web API controller by returning JSON data, but the design effect is not purely a rest API.
Perhaps Microsoft is also aware of the parallel provision of two sets of technology stack, not only to their own development to bring maintenance difficulties, but also to the user's learning and development difficulties. So, in ASP. NET 5, the two are unified on the technology stack, that is, the Web API can be developed in MVC. The most important embodiment of such a technology fusion is the mechanism of route mapping. Regarding the routing mechanism in ASP. NET 5, I will not go into the details, you can refer to other materials, such as "Interpretation of the ASP 5 & MVC6 Series (one): Routing route" (http://www.cnblogs.com/TomXu/p/ 4496462.html).
To make a spa example, I tried to use a hybrid controller to support both view processing and Rest data interface processing, with the benefit of not having to create two separate controllers for the MVC and Rest APIs, and to conform to both the action and routing styles of the MVC and Rest APIs. It may be difficult to describe the characteristics of a hybrid controller with text, or to look directly at the code.
Public classusercontroller:controller{PrivateAccountmanager _accountmanager; PublicUsercontroller (Accountmanager accountmanager) {_accountmanager = Accountmanager; }//The following is the MVC action //GET:/<controller>/ PublicIactionresult Index () {returnView (); } PublicIactionresult Edit (stringID) {viewbag.id = ID;returnView (); }//Below is the API action[HttpGet ("[Controller]/api])] PublicAsync task<basepaginglistdto<userdto>> GetAll (intpage = 1,intPageSize = ten) {var query = _accountmanager.usermanager.users; var total = await query. Countasync (); var users = await query. Skip ((page-1) * pageSize). Take (pageSize). Tolistasync (); var dto =NewBasepaginglistdto<userdto> (); Dto. Items = users. ConvertAll (user = mapper.map<userdto> (user)); Dto. Total = total;returnDto } [HttpGet ("[Controller]/api/{id}")] PublicAsync task<iactionresult> GetById (stringID) {var user = await _accountmanager.usermanager.findbyidasync (ID);if(User = =NULL)returnHttpnotfound (); var dto = mapper.map<userdto> (user);return NewObjectresult (DTO); } [Httpput ("[Controller]/api/{id}")] PublicAsync task<iactionresult> Editbyid (stringID, userdto dto) {if(modelstate.isvalid) {var user = await _accountmanager.usermanager.findbyidasync (ID);if(User = =NULL)returnHttpnotfound (); Dto. SetValue (user); Await _accountmanager.usermanager.updateasync (user);return NewHttpstatuscoderesult ((int) httpstatuscode.accepted); }return NewHttpstatuscoderesult ((int) httpstatuscode.badrequest); }}
In the above code, the Index action returns a view (index.cshtml) to the user. In this cshtml file, the main use of layout.cshtml to obtain a unified master, with only a few razor code, and use jquery and knockout to load and dynamic presentation of data. Where the data is loaded by GetAll This action, get the address or take advantage of "@Url. Action (" GetAll ")" came, and the actual access is get "/user/api".
Similarly, the edit action returns a view (edit.cshtml) that is used to edit the user's information. The user's single data is loaded by GetByID this action, and the actual access is get "/user/api/[userid". When the data needs to be modified, the action is to use Editbyid, the actual access address is put "/user/api/[userid]".
The code for this controller can also be viewed by code snippets: HTTP://GIT.OSCHINA.NET/IKE/CODES/8AVTL3U2P6G1IK09C5SNZ
ASP. 5 Adventure (7): Use a hybrid controller for easy single-page applications