How to call different methods in a controller in OData - odata

I am using web api 2.2 odata v4.0. I have a controller which has 3 methods.
For example GetA(), GetB() and GetC(). Which code should I use so that I will be able to call individual methods from the url ?
Also, how can I call a method GetA()? - as Get() is the default method that is being called in ODataController.
I used the code,
ODataRoute route = config.Routes.MapODataServiceRoute("odata", "odata",GetModel());
route.MapODataRouteAttributes(config); // This line threw an error sowing route does not have the method
MapODataRouteAttributes()
Please suggest me the solution.
Thanks

What do you need GetA(), GetB() and GetC() to do ? Are they for getting specific properties A, B and C? If so, you can take a look at the ODAtaAttributeRoutingSample. And if you want to know more about Web API OData, you can take a look at Sample Service implemented with Web API which is a complete project.

You have to work with the Action Attributes to specify other functions then those following the default rules.
routing with attributes

Related

OGNL needs to call a method

I need to call a method from my action object inside the JSP, something like:
var currentStatus = ${getCurrentStatus()};
I cannot call an attribute, and I tried following this answer (How to call an action method using OGNL) and it didn't work.
There are a variety of ways to call methods (on actions, on other objects, or static methods from classes) from OGNL.
In this case, however, I don't see any issue with using a normal accessor. Note that the JavaBean convention is almost (completely?) about naming. A getter named getCurrentStatus(), accessed simply in OGNL via currentStatus, can contain whatever code you want.
This could include the DB access you mention in your question, etc.

Add custom OData 'built in' methods to ODataServer endpoint

OData has it's own built in methods, like 'contains' 'endswith' 'geo.intersects' and so on..
Is there a way to add my own custom method? So I could give it a name, parameters and the Expression I want it to be translated to.
Thank you!
If you're using C#, you can go with your custom methods with [WebGet] annotation and IQueryable as the result. Then you need to register them by SetServiceOperationRule on DataServiceConfiguration. After that you can go with api/service.svc/YourMethodName. Hope it helps.

Is it possible to hack the spring-security-core plugin #Secured annotation to be able to reference request params given to a controller?

I am using Grails 2.3.3 and spring-security-core:2.0-RC4 plugin.
I am trying to protect a controller action by securing it depending on the result of a method call from a service that takes a parameter. This parameter should be something inside the request parameters.
I'd like to be able to do the following:
#Secured("#mySecurityService.myCustomCheck(params.id)")
def myAction(){
//do some things
}
I managed to be able to do the following:
#Secured("#mySecurityService.myCustomCheck()")
but now I have no idea how to access the request parameters that are sent to the controller.
Is it even architecturally possible to reference params variables inside the #Secured notation?
PS: I know you'll ask me to use spring-security-acl plugin. My problem is that it also adds a bunch of other things that I don't think I require.
In 2.0 you can use a closure as the annotation's check; there's a brief writeup and example in the docs: https://grails-plugins.github.io/grails-spring-security-core/v2/guide/newInV2.html
You'd express your example as this:
#Secured(closure={
ctx.mySecurityService.myCustomCheck(
request.getParameter('id'))
})
Return true to allow access.
Note that the ApplicationContext is available as the ctx variable, and the request as request; this is in addition to the other variables and methods that are available when using SpEL (see the Spring Security docs for details). params isn't available, but you can access values using request.getParameter

c# trigger pre and post method calls like mvc actionfilters

Is there a way to get the functionality of action filters in asp.net mvc where you can call and manipulate action parameters pre the method executing and post method call to update/view the return type data but in a standard c# class library class method?
This sounds a lot like aspect-oriented programming. PostSharp is an example of a tool that can help you get there. A common example is to have a class auto-implement INotifyPropertyChanged, which is boring or tedious, or to spit out a bunch of logging statements before and after a method call, without having to manually write that instrumentation in code.
Hope that gets you on the right track.

How to write test case for mvc action filters?

I have an action filter which i got from the below link
http://blog.wekeroad.com/blog/aspnet-mvc-securing-your-controller-actions/
there is something called "RequiresAuthenticationAttribute"
for this i need to write test case.
how can i do this? form some of the blogs i read that we need to mock httcontext.
How can i mock this? what is the procedure that i need to do? is there any link for this?
Don't use the [RequiresAuthentication] attribute from Rob's blog. It's meant for a very old pre-release version of MVC. Use the in-box [Authorize] attribute instead.
Since the [Authorize] attribute is written by the MVC team, you don't need to unit test its logic. However, if you want, you can verify that it's applied to your controllers or actions. Simply get the Type or MethodInfo you're interested in, then call its GetCustomAttributes() method to get instances of AuthorizeAttribute. You can inspect those instances for the values that you expect.
If you want, you can look at the source code of AuthorizeAttribute for information on writing your own filter. Additionally, you can look at the official unit test of this type, so if you do end up writing a filter you can use a similar method to write your own type's unit tests.

Resources