I'll start off by saying I'm new to OData. I do have a basic controller set up and working in my environment. I need to do a bit of custom work now. Before my OData service returns it's result set, I need to run a check on that data before I send it back
to the client. So some custom code in the controller I assume. Every time this is called, I need to run this. In other words, there are no cases when this piece of data checking will not be done before returning data to the client. Is there a method I can
override in ODataController or can I just throw the code into the controller I've created? (The Get?). I want to run this on the subset of data that has been filtered out by OData already.
Before my OData service returns it's result set, I need to run a check on that data before I send it back to the client.
To achieve the above requirement, as you mentioned, you can implement custom code (or call custom function) in controller action. Besides, you can achieve same with ActionFilter, like below.
public class RCheckerFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var objectContent = actionExecutedContext.Response.Content as ObjectContent;
if (objectContent != null)
{
// the returned value
var value = objectContent.Value;
//code logic here
}
}
}
With Regards,
Fei Han
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
public class TradeDataCheck : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var objectContent = actionExecutedContext.Response.Content as ObjectContent;
if (objectContent != null)
{
// the returned value
var value = objectContent.Value;
//code logic here
}
}
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
}
}
Any suggestions? I need it to run before data is ever queried by the request itself (so I can run a data check before to ensure data is in a specific format already)
Do you mean that you set breakpoint inside TradeDataCheck class and debug the code, which do not reach into code logic while you make request to action? Does it directly return action result to client while you make a GET request, or cause any error?
The code snippet work well on my side, please try to create a new Web API application and test if it work well.
With Regards,
Fei Han
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
You can try to add the action filter within WebApiConfig class's Register method and check if it work for you.
config.Filters.Add(new RCheckerFilter());
With Regards,
Fei Han
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
442 Points
216 Posts
OData 4
Oct 08, 2019 09:20 PM|Mike Bell|LINK
I'll start off by saying I'm new to OData. I do have a basic controller set up and working in my environment. I need to do a bit of custom work now. Before my OData service returns it's result set, I need to run a check on that data before I send it back to the client. So some custom code in the controller I assume. Every time this is called, I need to run this. In other words, there are no cases when this piece of data checking will not be done before returning data to the client. Is there a method I can override in ODataController or can I just throw the code into the controller I've created? (The Get?). I want to run this on the subset of data that has been filtered out by OData already.
Thank you in advance!
Michael
My Code Blog
All-Star
40565 Points
6233 Posts
Microsoft
Re: OData 4
Oct 09, 2019 07:02 AM|Fei Han - MSFT|LINK
Hi Michael,
To achieve the above requirement, as you mentioned, you can implement custom code (or call custom function) in controller action. Besides, you can achieve same with ActionFilter, like below.
With Regards,
Fei Han
Member
442 Points
216 Posts
Re: OData 4
Oct 09, 2019 03:02 PM|Mike Bell|LINK
This is exactly the kind of information I needed. I will try this later today. Thank you!
My Code Blog
Member
442 Points
216 Posts
Re: OData 4
Oct 10, 2019 06:13 PM|Mike Bell|LINK
My Attribute code never seems to get called:
Any suggestions? I need it to run before data is ever queried by the request itself (so I can run a data check before to ensure data is in a specific format already)
My Code Blog
All-Star
40565 Points
6233 Posts
Microsoft
Re: OData 4
Oct 11, 2019 06:41 AM|Fei Han - MSFT|LINK
Hi Michael,
Do you mean that you set breakpoint inside TradeDataCheck class and debug the code, which do not reach into code logic while you make request to action? Does it directly return action result to client while you make a GET request, or cause any error?
The code snippet work well on my side, please try to create a new Web API application and test if it work well.
With Regards,
Fei Han
Member
442 Points
216 Posts
Re: OData 4
Oct 11, 2019 06:08 PM|Mike Bell|LINK
I ended up just calling a normal web service. Time crunch, had to move on. I had set breakpoints that never got hit.
My Code Blog
All-Star
40565 Points
6233 Posts
Microsoft
Re: OData 4
Oct 14, 2019 05:03 AM|Fei Han - MSFT|LINK
Hi Michael,
You can try to add the action filter within WebApiConfig class's Register method and check if it work for you.
With Regards,
Fei Han