Mark one or many routes to a web api controller endpoints as deprecated/obsolete #8258
Replies: 2 comments 3 replies
-
This is (1) not a language issue, but an ASP one, and (2) is not possible because it would change how attributes work. Attributes apply to the method, so your proposal would mark the method as obsolete regardless of where the attribute is placed. Also, why are you marking API route methods obsolete? That doesn't do anything to the client. If you want one method that handles both routes, just put the [HttpGet("v1/auditHistory")]
[HttpGet("v2/auditHistory")]
public IActionResult GetAuditHistory() => Ok(""); If you want separate methods, you don't need to duplicate code. Just forward the method call! If you ever find yourself duplicating code, ask yourself if there's an easier solution. [HttpGet("v1/auditHistory")]
[Obsolete]
public IActionResult GetAuditHistoryOld() => GetAuditHistory();
[HttpGet("v2/auditHistory")]
public IActionResult GetAuditHistory() => Ok(""); |
Beta Was this translation helpful? Give feedback.
-
Combining attributes in this manner is already legal: [Foo]
[Bar, Baz]
public void M() { } It means the same thing as the following: [Foo]
[Bar]
[Baz]
public void M() { }
// or
[Foo, Bar, Baz]
public void M() { } If you're looking for a way to denote that a particular application of an attribute is obsolete, I would make a request to the owner of that project to include an [Foo(Obsolete = true)]
[Bar]
[Baz]
public void M() { } |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In web api - where there are multiple route attributes on a controller method, can we please have the ability to mark one or many of the routes as obsolete/deprecated Currently to achieve this I have to duplicate the method and make one of the methods obsolete/deprecated. I must have one method for obsolete/deprecated routes, and one method for non obsolete/deprecated routes. I have hundreds of endpoints like this in my app. The code duplication is horrible,
Current code with duplication:
`[ApiController]
[Route("api/[controller]")]
public class AuditController : ControllerBase
{
}`
New suggestion:
[ApiController] [Route("api/[controller]")] public class AuditController : ControllerBase { [HttpGet("v1/auditHistory"), Obsolete] [HttpGet("v2/auditHistory")] public IActionResult GetAuditHistory() { return Ok("This route output is the same for both routes, but one route is obsolete"); } }
Thank you :)
Beta Was this translation helpful? Give feedback.
All reactions