14
votes
Accepted
ASP.NET REST controller with try-catch error handling
throw ex;
This is a fatal mistake. It'll create a new stack-trace and you won't be able to tell where the actual exception occured. If you want to rethrow it then ...
13
votes
C# Am I using proper SOLID principles?
I suppose the simplest thing is to go through the principles:
SRP - Cannot see any 'too big' things in your example
OCP - The example is small enough and you are not using class
inheritance that I ...
6
votes
Accepted
WebApi Get with paging
Let's start with the BUG.
if (entity.Count() == 0 || entity == null)
If entity is null, ...
6
votes
Connect to MySQL and fetch a list of customers asynchronously using C#
In Get() you do not need to wrap the call to GetAllCustomers() into a Task because it's ...
6
votes
C# Am I using proper SOLID principles?
IMO returning IEnumerable<T> from an API is a violation of "L", as running GetEnumerator() a second time (or ...
5
votes
Accepted
Method GetById and SingleResult
You can wrap the single entity into an IQueryable. Something like this should work:
SingleResult.Create(new[]{entity}.AsQueryable());
You could write a helper ...
5
votes
Accepted
N-tiered web app to save a role
Naming conventions.
In C#, types are named using PascalCase. Rename:
OM_Role to OmRole
...
5
votes
Generic WebRequest Method for .PCL
Thing that makes me more perplex is error-handling.
First of all you're catching all exceptions, it may hide subtle bugs unrelated to web request itself (for example wrong parameters you supply in ...
5
votes
ASP.NET REST controller with try-catch error handling
Typically in an ASP.NET app (and in fact in any app), you should only catch and handle the base System.Exception in a single place as close to the application entry ...
5
votes
Custom HttpClient implementation for third part usage with sync/async calls
internal class Client : IDisposable
{
private static HttpClient _client;
private static Uri _baseAddress;
Neither of those fields should be ...
4
votes
.NET REST API JSON Filtering
I know this is an old post but I tried doing something similar recently so thought I should share it here. The code looks great but it still returns unwanted properties with 'Null' values. What I have ...
4
votes
Method GetById and SingleResult
[EnableQuery] and SingleResult does in fact return 404 OOTB, that is one of the key reasons for ...
4
votes
C# Async Oauth API Wrapper Class
Do not use a public field; instead, make it a property:
public bool isInitialized = false;
Your code is barely above 100 lines, you don't need ...
4
votes
Generic WebRequest Method for .PCL
I would define it as an extension to the Uri type, so it could be used this way:
...
4
votes
Accepted
Claiming coupons
Short answer is maybe. If this is the only code that updates it then you will be fine but you are making a gateway that only one person can update at a time even if they have different codes. If your ...
4
votes
Web API for reading file system
It's a very bad idea to call the controller a FileSystem and inform the user that he's going to be working with well, a file system. He should not know how you are ...
4
votes
Accepted
Web API for reading file system
I think one of the most common errors in today times, is for example handling potential errors, as you try to do here:
...
4
votes
Accepted
Reading MultiPart From Data with descriptors
The naming and variable conventions in your method look like it was written by at least three different persons:
...
4
votes
Accepted
IsDatabaseUp returns true or throws exception
The naming convention Is… implies that the function is a predicate, returning either true or false.
If you wanted to throw an exception when the database is down, ...
4
votes
Accepted
Setting up pagination links inside a WebAPI Controller
The intention
When I have all matching objects - I pass them to a "Paging" method within the service (I think I have to do this in the service rather than the repository because a generic repo ...
4
votes
Accepted
Trying to map JSON to different classes in web API
The first thing to do is to remove duplicated code. If you do not need further processing DeserializeObject<T>() returns T ...
4
votes
ASP.NET REST controller with try-catch error handling
The linked post adds a neat summary:
So, to sum up:
Don’t catch fatal exceptions; nothing you can do about them anyway, and trying to generally makes it worse.
Fix your code so that it ...
4
votes
Subscribe to Event form one service to another
in your case I would try to avoid subscribing to events as this couples the projects and references and prevents the garbage collector to clean up the unused service. if you are creating new services ...
3
votes
Accepted
API controller with database queries
I see two improvements, one of which Gert Arnold pointed on in a comment.
If an API method returns a list, collection or IEnumerable of anything, worse case scenario you should return an empty ...
3
votes
Accepted
Multiple connection string in Web API 2
First of all I would highlight a security problem: you're directly exposing your implementation to service users. They just need to try different Client-IDs to ...
3
votes
Accepted
Building a custom response body based on API consumer's need
Possible problem
Assume one would pass a request like
api/characters/2?fields=id,displayname,mainImageUrl,displayname
then the Assemble<TEntity, TDto>() ...
3
votes
Accepted
Implementation Async/Await
Your implementation is technically correct, but...
There is no benefit in using Task.Run in an web application.
All you're doing is deferring work from one thread ...
3
votes
Accepted
WebApi action that updates entity using stored procedure
You don't have a people, you have a person.
Saying if (person.IsMain == true) is exactly ...
3
votes
Accepted
Choose the right type for method GetById
If you're only going to make the minimal required change
Accommodate the IQueryable requirement in the layer where it is needed, not in your repository layer. That will probably be some object that ...
3
votes
MVC-Web API 2 integration
Avoid duplicated magic strings, like "http://www.ortund.com/". Assign to a constant instead.
This snippet appears repeated multiple times:
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
asp.net-web-api × 141c# × 136
entity-framework × 17
asp.net-mvc × 15
.net × 14
asp.net × 14
rest × 11
asp.net-core × 11
repository × 9
async-await × 8
dependency-injection × 6
xamarin × 6
design-patterns × 5
error-handling × 5
api × 5
authentication × 5
javascript × 4
http × 4
asp.net-mvc-4 × 4
jwt × 4
ninject × 4
owin × 4
json × 3
controller × 3
.net-core × 3