Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8585705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:07:59+00:00 2026-06-11T22:07:59+00:00

If a requested resource is not found by the Service Layer returning null to

  • 0

If a requested resource is not found by the Service Layer returning null to the Web API controller; what is the best way to throw a HttpStatusCode.NotFound response back to the client without hard coding it in the controller, and by checking if it’s null?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:08:01+00:00Added an answer on June 11, 2026 at 10:08 pm

    Personally I would just do the checks in the controllers as per Oppositional’s comment but what you are asking for is perfectly reasonable. Again using action filters either attached per controller (or registered globally) you could do something along these lines:

    Example Model:

    public class Foo
    {
        public string Bar { get; set; }
    }
    

    Example Controller:

    public class FoosController : ApiController
    {
        [NullObjectActionFilter]
        public Foo Get(string id)
        {
            // - Returns model and 200
            //return new Foo() { Bar = "TEST" };
    
            // - Returns 404
            //return null;
        }
    }
    

    The Filter:

    public class NullObjectActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            object outValue = null;
            actionExecutedContext.Response.TryGetContentValue<object>(out outValue);
            if (outValue == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
    
            base.OnActionExecuted(actionExecutedContext);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have been requested to stream a single video (+500MB) from a web site.
We have a RESTful web service with a URI pattern like: /orders/ /orders/1 /orders/1/changelog
I've been trying to create a Web Service with play framework. I've searched some
i'm constructing a web-service that is used, in this particular case, to ask for
Description: An error occurred during the compilation of a resource required to service this
I have a problem with a web service. I add a web service reference
I have written an ASP.NET web application (not site) in Delphi Prism. Everything works
If something goes wrong in a WCF REST call, such as the requested resource
I have a web service application that I deployed successfully on Apache Tomcat using
I want to use the web service in my app and upload post on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.