Base solution for your next web application
Open Closed

Restful and 404 Not Found #4995


User avatar
0
oscargonzalezmoreno created

Hello, I'm starting with ASP.NET Zero. I'm developing a Restful service, and I don't see how I can return a simple 404 with a simple JSON. I have the following code, and when I execute it with Swagger, the return JSON is OK, but I got an HTTP 500 error code. All I want is a 404 error code.

Kind regards, Oscar

[HttpGet] [Route ("api/services/v1/boschema/{uuid}")] public async Task<SdBoSchemaDto> Get (string uuid) { SdBoSchema entity; try { entity = _sdBoSchemaRepository.GetAll ().First (e => e.UUID == uuid); } catch (Exception e) { throw new UserFriendlyException (404, "Item Not Found"); }

        return ObjectMapper.Map&lt;SdBoSchemaDto&gt; (entity);
    }

1 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    You can throw EntityNotFoundException:

    try
    {
        entity = _sdBoSchemaRepository.GetAll().First(e => e.UUID == uuid);
    }
    catch (Exception e)
    {
        // throw new EntityNotFoundException(typeof(SdBoSchema), uuid);
        throw new EntityNotFoundException("Item Not Found");
    }