Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a 304 result from ASP.NET Core web api?

Tags:

asp.net-core

I've been returning HTTP status codes from within my ASP.NET Core web api as follows:

// Return code 200:
return Controller.Ok(someOptionalMessageHere);

I now have the need to return a status code 304 (i.e. not modified), but I don't see anything from intellisense in the "Microsoft.AspNetCore.Mvc.Controller" class that does that, nor do those namespaces appear in the online API browser (?). What is the proper way to do this?

like image 469
LKeene Avatar asked Dec 17 '25 21:12

LKeene


2 Answers

As far as i know there is no explicit method for 304.

But you could use

return  Controller.StatusCode(304,ObjectYouWantToReturn);

Edit: Here is a link to the documentation

like image 60
Marius Junak Avatar answered Dec 20 '25 16:12

Marius Junak


As the eTag specification says you don't return content for a 304 (not modified) I suggest you do this instead:

return StatusCode((int)HttpStatusCode.NotModified);
like image 45
Gone Coding Avatar answered Dec 20 '25 16:12

Gone Coding