Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating REST API with custom "x-myvalue" in header

Super simple ApiController:

[ApiController]
[Route("[controller]")]
public class MyController : ControllerBase
{
  [HttpPost]
  public IActionResult DoStuff([FromHeader] string X-SomeValue)
  {
     return Ok();
  }

}

This does not compile, as X-SomeValue is not a valid parameter name.
The easiest is just call it XSomeValue, but AFAIK best practise is use X-.

How can I use X-SomeValue in the header?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To use names that are not valid C# identifiers, you must specify the name explicitly like this:

  [HttpPost]
  public IActionResult DoStuff([FromHeader(Name = "X-SomeValue")] string xSomeValue)
  {
     return Ok();
  }

More information on the FromHeader attribute’s Name property:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading