Skip to content

eSchoool/multi-client-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Building multi-client APIs in ASP.NET

multi-client-api-06
The project introduces a method that processes JTokens in order to return only the properties requested by the client.
  • Usage
Assuming the result of the resource uri /api/tracks/1 is the following: ``` { "TrackId": 1, "AlbumId": 1, "Bytes": 11170334, "Composer": "Angus Young, Malcolm Young, Brian Johnson", "GenreId": 1, "MediaTypeId": 1, "Milliseconds": 343719, "Name": "For Those About To Rock (We Salute You)", "UnitPrice": 0.99 } ``` You can request only specific properties of that resource by making the request /api/tracks/1?props=bytes,milliseconds,name ``` { "Bytes": 11170334, "Milliseconds": 343719, "Name": "For Those About To Rock (We Salute You)" } ``` The algorithm supports nested navigation properties as well. If /api/albums/1 returns.. ``` { "AlbumId": 1, "ArtistName": "AC/DC", "Title": "For Those About To Rock We Salute You", "Track": [ { "TrackId": 1, "AlbumId": 1, "Bytes": 11170334, "Composer": "Angus Young, Malcolm Young, Brian Johnson", "GenreId": 1, "MediaTypeId": 1, "Milliseconds": 343719, "Name": "For Those About To Rock (We Salute You)", "UnitPrice": 0.99 }, { "TrackId": 6, "AlbumId": 1, "Bytes": 6713451, "Composer": "Angus Young, Malcolm Young, Brian Johnson", "GenreId": 1, "MediaTypeId": 1, "Milliseconds": 205662, "Name": "Put The Finger On You", "UnitPrice": 0.99 } ] } ``` Then /api/albums/1?props=artistname,title,track(composer;name) should return the following: ``` { "ArtistName": "AC/DC", "Title": "For Those About To Rock We Salute You", "Track": [ { "Composer": "Angus Young, Malcolm Young, Brian Johnson", "Name": "For Those About To Rock (We Salute You)" }, { "Composer": "Angus Young, Malcolm Young, Brian Johnson", "Name": "Put The Finger On You" } ] } ``` Properties in navigations should be semicolon (;) separated inside parethensis.
  • Example in API Controller
``` var _tracks = _trackRepository.GetAll(includeProperties).Skip(page).Take(pageSize);
            var _tracksVM = Mapper.Map<IEnumerable<Track>, IEnumerable<TrackViewModel>>(_tracks);

            string _serializedTracks = JsonConvert.SerializeObject(_tracksVM, Formatting.None,
                new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });

            JToken _jtoken = JToken.Parse(_serializedTracks);
            if (!string.IsNullOrEmpty(props))
                Utils.FilterProperties(_jtoken, props.ToLower().Split(',').ToList());

            return Ok(_jtoken);
<p>
The project is built in Visual Studio 2015 and ASP.NET Core but the technique and the method can be easily integrated in any version of ASP.NET API. In case you want to run the <i>ShapingAPI</i> application:
<ol>
<li>
Download the source code and open the solution in Visual Studio 2015
</li>
<li>
Restore Nuget and Bower packages
</li>
<li>
Install the <a href="https://chinookdatabase.codeplex.com/" target="_blank">Chinook</a> database in your SQL Server by running the script inside the <a href="https://github.com/chsakell/multi-client-api/tree/master/src/ShapingAPI/SQL" target="_blank">SQL</a> folder.
</li>
<li>
Alter the <i>appsettings.json</i> file to reflect your database environment.
</li>
<li>
Run the application
</li>
</ol>
</p>

<h3 style="font-weight:normal;">Follow chsakell's Blog</h3>
<table id="gradient-style" style="box-shadow:3px -2px 10px #1F394C;font-size:12px;margin:15px;width:290px;text-align:left;border-collapse:collapse;" summary="">
<thead>
<tr>
<th style="width:130px;font-size:13px;font-weight:bold;padding:8px;background:#1F1F1F repeat-x;border-top:2px solid #d3ddff;border-bottom:1px solid #fff;color:#E0E0E0;" align="center" scope="col">Facebook</th>
<th style="font-size:13px;font-weight:bold;padding:8px;background:#1F1F1F repeat-x;border-top:2px solid #d3ddff;border-bottom:1px solid #fff;color:#E0E0E0;" align="center" scope="col">Twitter</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="4" style="text-align:center;">Microsoft Web Application Development</td>
</tr>
</tfoot>
<tbody>
<tr>
<td style="padding:8px;border-bottom:1px solid #fff;color:#FFA500;border-top:1px solid #fff;background:#1F394C repeat-x;">
<a href="https://www.facebook.com/chsakells.blog" target="_blank"><img src="https://chsakell.files.wordpress.com/2015/08/facebook.png?w=120&amp;h=120&amp;crop=1" alt="facebook" width="120" height="120" class="alignnone size-opti-archive wp-image-3578"></a>
</td>
<td style="padding:8px;border-bottom:1px solid #fff;color:#FFA500;border-top:1px solid #fff;background:#1F394C repeat-x;">
<a href="https://twitter.com/chsakellsBlog" target="_blank"><img src="https://chsakell.files.wordpress.com/2015/08/twitter-small.png?w=120&amp;h=120&amp;crop=1" alt="twitter-small" width="120" height="120" class="alignnone size-opti-archive wp-image-3583"></a>
</td>
</tr>
</tbody>
</table>

About

Building multi-client APIs in ASP.NET

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 84.1%
  • JavaScript 8.6%
  • HTML 6.6%
  • CSS 0.7%