AspNetCoreOData
AspNetCoreOData copied to clipboard
Unable to override location in response header.
Assemblies affected ASP.NET Core OData 8.2.0
Describe the bug We want to add specific url in location response header, but we are unable to override. I looked into the code and found that it is happening due to below code. (https://github.com/OData/AspNetCoreOData/blob/2bde4e5994dcc049c0a924fefc4663c1b5341247/src/Microsoft.AspNetCore.OData/Results/CreatedODataResult.cs#L55)
Can you create a new class derived from CreatedODataResult and override the method, call base first then update the Location header for the response?
Will it be possible for you to share an example?
Get Outlook for iOShttps://aka.ms/o0ukef
From: Sam Xu @.> Sent: Wednesday, August 30, 2023 11:49:58 PM To: OData/AspNetCoreOData @.> Cc: Isha Tyagi @.>; Author @.> Subject: Re: [OData/AspNetCoreOData] Unable to override location in response header. (Issue #1034)
Can you create a new class derived from CreatedODataResult and override the method, call base first then update the Location header for the response?
— Reply to this email directly, view it on GitHubhttps://github.com/OData/AspNetCoreOData/issues/1034#issuecomment-1699639840, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A2KUX655BOBUPUNZ5PS5ZADXX5745ANCNFSM6AAAAAA4EMPDG4. You are receiving this because you authored the thread.Message ID: @.***>
@ishatyagiit could you elaborate why you need to customize this one? OData uses proper REST conventions to set this header so I'm curious why you'd want to change it.
Hi Juliano,
We are working on exposing Viva Goals Export APIs to graph where one of the create APIs has location header in response. API call: POST https://graph.microsoft.com/beta/employeeexperience/goals/exportJobs
HTTP/1.1 201 Created Content-type: application/json Location: "https://graph.microsoft.com/beta/employeeexperience/goals/exportJobs/eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI4MzIxMjc1In0"
```json
{
"id": "eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI4MzIxMjc1In0",
"status": "notStarted",
"createdDateTime": "2023-06-19T12-06-03.0024Z"
}
You can check the below link for more details:
https://msazure.visualstudio.com/One/_git/AD-AggregatorService-Workloads?path=/Reviews/8561460-18509-Export-APIs-for-Viva-Goals/API.md&version=GBmaster&line=67&lineEnd=67&lineStartColumn=2&lineEndColumn=10&lineStyle=plain&_a=contents
Thanks,
Isha Tyagi
________________________________
From: Juliano Leal Goncalves ***@***.***>
Sent: 31 August 2023 01:28
To: OData/AspNetCoreOData ***@***.***>
Cc: Isha Tyagi ***@***.***>; Mention ***@***.***>
Subject: Re: [OData/AspNetCoreOData] Unable to override location in response header. (Issue #1034)
@ishatyagiit<https://github.com/ishatyagiit> could you elaborate why you need to customize this one? OData uses proper REST conventions to set this header so I'm curious why you'd want to change it.
—
Reply to this email directly, view it on GitHub<https://github.com/OData/AspNetCoreOData/issues/1034#issuecomment-1699756395>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2KUX65647T3AC3D7CYB3F3XX6LPFANCNFSM6AAAAAA4EMPDG4>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
Could you help us to understand that how to override location in response header?
Thanks
From: Isha Tyagi @.> Sent: 31 August 2023 15:02 To: OData/AspNetCoreOData @.>; OData/AspNetCoreOData @.> Cc: Mention @.> Subject: Re: [OData/AspNetCoreOData] Unable to override location in response header. (Issue #1034)
Hi Juliano,
We are working on exposing Viva Goals Export APIs to graph where one of the create APIs has location header in response. API call: POST https://graph.microsoft.com/beta/employeeexperience/goals/exportJobs
HTTP/1.1 201 Created Content-type: application/json Location: "https://graph.microsoft.com/beta/employeeexperience/goals/exportJobs/eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI4MzIxMjc1In0"
```json
{
"id": "eyJfdHlwZSI6Ikdyb3VwIiwiaWQiOiI4MzIxMjc1In0",
"status": "notStarted",
"createdDateTime": "2023-06-19T12-06-03.0024Z"
}
You can check the below link for more details:
https://msazure.visualstudio.com/One/_git/AD-AggregatorService-Workloads?path=/Reviews/8561460-18509-Export-APIs-for-Viva-Goals/API.md&version=GBmaster&line=67&lineEnd=67&lineStartColumn=2&lineEndColumn=10&lineStyle=plain&_a=contents
Thanks,
Isha Tyagi
________________________________
From: Juliano Leal Goncalves ***@***.***>
Sent: 31 August 2023 01:28
To: OData/AspNetCoreOData ***@***.***>
Cc: Isha Tyagi ***@***.***>; Mention ***@***.***>
Subject: Re: [OData/AspNetCoreOData] Unable to override location in response header. (Issue #1034)
@ishatyagiit<https://github.com/ishatyagiit> could you elaborate why you need to customize this one? OData uses proper REST conventions to set this header so I'm curious why you'd want to change it.
—
Reply to this email directly, view it on GitHub<https://github.com/OData/AspNetCoreOData/issues/1034#issuecomment-1699756395>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2KUX65647T3AC3D7CYB3F3XX6LPFANCNFSM6AAAAAA4EMPDG4>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
Let me create a derived class from 'CreatedODataResult< T >'.
public class MyCreatedResult<T> : CreatedODataResult<T>
{
private string _uri = null;
public MyCreatedResult(T entity) : base(entity)
{
}
public MyCreatedResult(string uri, T entity) : base(entity)
{
_uri = uri;
}
public async override Task ExecuteResultAsync(ActionContext context)
{
if (_uri != null)
{
context.HttpContext.Response.Headers["Location"] = _uri;
ObjectResult objectResult = new ObjectResult(Entity)
{
StatusCode = StatusCodes.Status201Created
};
await objectResult.ExecuteResultAsync(context).ConfigureAwait(false);
}
else
{
await base.ExecuteResultAsync(context);
}
}
}
Then, use it in controller:
public class CustomersController : ODataController
{
[HttpPost]
public IActionResult Post(Customer customer)
{
string uri = "https://any";
// ...
return new MyCreatedResult<Customer>(uri, customer);
}
}
Then, it can work:
Thank you. It worked
@xuzhg / @ishatyagiit just keep in mind that that implementation will stop honoring the return=minimal preference header when passing the custom url in (as well as stop emiting the "EntityId" and "ServiceVersion" headers.
I'd recommend doing it in a slightly different way to avoid that change in behavior and use more of the core logic from CreatedODataResult<T>:
- https://github.com/OData/AspNetCoreOData/blob/fce1c6121a2a88b5e34d650ed609b913415a6c68/src/Microsoft.AspNetCore.OData/Results/CreatedODataResult.cs#L25
Perhaps something like this would be more appropriate:
public class MyCreatedResult<T> : CreatedODataResult<T>
{
private Uri? uri = null;
public MyCreatedResult(T entity) : base(entity)
{
}
public MyCreatedResult(Uri uri, T entity) : base(entity)
{
this.uri = uri;
}
public async override Task ExecuteResultAsync(ActionContext context)
{
await base.ExecuteResultAsync(context);
if (this.uri is not null && context.HttpContext.Response is { StatusCode: 201 } response)
{
response.GetTypedHeaders().Location = this.uri;
}
}
}
I changed the argument to Uri to match the strongly-typed interface from GetTypedHeaders (which I'd also recommend instead of manually accessing the headers with hardcoded strings).
Hi @julealgon / @xuzhg,
Do we have plan to convert this feature request ?