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 9205491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:57:43+00:00 2026-06-17T23:57:43+00:00

I want to know if it is possible or how can I mark fields

  • 0

I want to know if it is possible or how can I mark fields on my class used as a parameter on my Web API call to be required? I obviously can do this manually once I have received the message, but I was hoping there was something built in the pipeline (like in MVC in combination with jQuery that uses required field annotations to automatically kick back to UI showing required field notations) so I don’t have to check everything manually.

Let’s say I have the following ViewModel class:

public class PersonViewModel
{
  public string FirstName {get; set;}

  public string MiddleName {get; set;}

  public string LastName {get; set;}

}

Here is my simple Post method on a PersonController

public HttpResponseMessage Post(PersonViewModel person)
{


}

Let’s say the FirstName and LastName fields are required but not MiddleName. What I want to know is will the call automatically respond back to the client with a HTTP 400 Bad Request or similar if the Person object does not have one of the required fields populated?

Essentially do I have to do all of this work manually, or is there a way to have the framework handle notated fields automatically, so I don’t have a lot of boilerplate validation code for required fields?

Manual Way I’m trying to avoid:

if (ModelState.IsValid)
{
  if (person.LastName == string.empty)
  {
     return Request.CreateResponse(HttpStatusCode.BadRequest);
  }

}

Any help is appreciated, thanks!

  • 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-17T23:57:45+00:00Added an answer on June 17, 2026 at 11:57 pm

    WebAPI does have a validation feature. You should be able to mark the FirstName and LastName properties as [Required] and then use the action filter at the bottom of this blog post to send back an appropriate response:

    http://blogs.msdn.com/b/youssefm/archive/2012/06/28/error-handling-in-asp-net-webapi.aspx

    You can read more about WebAPI validation here:

    http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can anyone recommend a simple Java web-app framework? I want to know
i want know if it's possible create a custom delegate in the AppDelegate class,
I want to know if its possible to hide a base class property from
i want know if is possible, to get a specific element value of a
I want to know is it possible to generate .raw file format using C++.
I want to know if its possible to choose a GC algorithm via code
i want to know if its possible to use a function like .delegate() ,
I want to Know is it possible to customize dial chart's background in Achart
i want to know if it possible to access the browser bookmarks via Flash.
I just want to know if is possible to calculate the diference between two

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.