Web API and OData v4

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
11/26/2014 5:40:42 AM
Gravatar
Total Posts 5

Web API and OData v4

Hi Joe,

Recently had a requirement to expose an OData service from within mojo, using Web API.

I implemented it fairly easily by using a HttpModule to register the routing, which runs only once. Wondered if there was a better way of registering routes - I think I read somewhere about registering routes using config files?

The service is running fine, no issues encountered so far.

Cheers,

Steve

12/1/2014 12:00:37 PM
Gravatar
Total Posts 18439

Re: Web API and OData v4

Hi Steve,

If you are using the latest version of mojoPortal, you can make your own calls that implements IRegisterRoutes, then plug it in by dropping a config file in the folder /Setup/RouteRegistrars, look at existing files there for format, and see an example implementation where we are using web api in the forums.

By plugging in your class it will be called at application start so your own code can register additional routes.

Prior to this we had another way to plugin routes by configuration of each route in xml files located under /Setup/Routes. That implementation was removed from mojoPortal in recent versions but for backward compatibility for a few people who were using we, we moved that into a separate class library project mojoPortal.RouteHelpers (in our repository), so if someone needs the old way to still work or liked it better they can compile that into a dll and drop it in the bin and it restores the old functionality but doesn't break the new way of registering routes either. There was also a file mojoRoutes.config in that /Setup/Routes folder with examples of how to register routes in comments within that file. But we were never actually using that for any features included in mojoPortal.

If you are using the latest version and don't have old routes configured the old way then I suggest use the new way, especially since you are able to write and compile your own code as you did with the http module. 

Hope that helps,

Joe

5/30/2015 2:57:43 AM
Gravatar
Total Posts 137
When enough isn't

Re: Web API and OData v4

Hi Joe,

A related question. I'm looking at the new stuff from Microsoft with Web.Api 2.2.

I just tried to add the nuget packages to a mojoportal website and it failed possibly due to conflicts.

How far do you believe you can go in the Web Api 2.2 direction using the latest mojoportal versions? Should I stay away from the nuget packages and make a more semi-automated and semi-far api solution? Maybe you have insights to direct me a bit, but anyway let me know your view to it.

I hope I can find the best way to supplement mojoportal with apiaccess to my data. In that way I can add mobile apps and/or light clients for specific uses.

Best regards
Lars

5/30/2015 7:07:26 AM
Gravatar
Total Posts 18439

Re: Web API and OData v4

NuGet is for source code not for deployment packages

mojoportal source code already has a dependency on the NuGet for Web API 2.2 and the needed dlls ships with the deployment package.

In my previous post on this thread I provided info about where it is used in the forums so you can see example code and how to plugin your own api routes

So as far as I know nothing is stopping you from using web api in your custom code

5/30/2015 12:53:28 PM
Gravatar
Total Posts 137
When enough isn't

Re: Web API and OData v4

Hi Joe,

Thanks for a quick response. This is very good news.

I will right away dig into it and get things going. Thanks a lot.

Lars

6/2/2015 9:50:29 AM
Gravatar
Total Posts 137
When enough isn't

Re: Web API and OData v4

Hi,

I'm digging my way through, to get the API working. But I keep getting some error messages in Fiddler, when I test it on my developer machine and I can't figure out the reason behind I'm hoping you might be able to send me in the right direction.

I'm working directly with the c# code on my developer machine, so I haven't made any builds, dlls or xml xxxxRoutes.config file - maybe this is why?

Here is my routes definition:
namespace MarathonAPI {     public class RouteRegistrar : IRegisterRoutes     {         public void Register(HttpConfiguration config)         {             // api routes             config.Routes.MapHttpRoute(                 name: "MarathonAPI",                 routeTemplate: "api/{controller}/{id}",                 defaults: new { id = RouteParameter.Optional }             );         }         public void RegisterRoutes(RouteCollection routes)         {             //mvc routes         }         public void RegisterGlobalFilters(GlobalFilterCollection filters)         {         }     } }
 

And here is the error message, where it seems to find my controller twice:
{"Message":"An error has occurred.","ExceptionMessage":"Multiple types were found that match the controller named 'races'. This can happen if the route that services this request ('api/{controller}/{id}') found multiple controllers defined with the same name but differing namespaces, which is not supported.\r\n\r\nThe request for 'races' has found the following matching controllers:\r\nMarathonAPI.RacesController\r\nRacesController","ExceptionType":"System.InvalidOperationException","StackTrace":"   at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}

It's ok if you can't, but maybe you can give some sorts of hint..

Best regards
Lars

6/2/2015 10:26:46 AM
Gravatar
Total Posts 18439

Re: Web API and OData v4

one problem is your route template matches the default route that is already registered. you can't register another route with the same pattern

routeTemplate: "api/{controller}/{id}"

your route template should be different and more specific, for example in the forum we registered a route with:

routeTemplate: "api/forummod/{id}"

also unless you have a xxxRoutes.config file then how will your RouteRegistrar code ever be called? that file is needed so that mojoPortal knows where to find your IRegisterRoutes implementation and how to execute it, without that it will never be called

a dll must exist in the bin also for your code to execute, ie you must build/compile it

6/2/2015 10:35:47 AM
Gravatar
Total Posts 137
When enough isn't

Re: Web API and OData v4

All right, thanks for super fast response!

I thought I had come around the double pattern, but that sounds as the reason for it finding it twice.

Actually I have many class-files running under my own namespaces to support my ascx-files. None of these are build/compiled - they just exist in the App_Code folder - and my site runs nicely. But I guess I have to learn that part too for the API to work.

I need to find a way to build only the api files using includes, I guess??

Thanks a lot Joe - you always get me going in the right direction.

Lars

6/6/2015 9:55:12 AM
Gravatar
Total Posts 137
When enough isn't

Re: Web API and OData v4

Thanks again, Joe

I think I got it working now.

I'm wondering of the best way to utililize the security and roles system of MojoPortal to control access to the API.

There is a lot of authentication methods with recommendations on OAuth and tokens. But what is the best way to align with the current Membershipmodel for MojoPortal? Is it like I see the code of ForumMod.cs and the IsAllowed function? I think this is if it's a user signed in.

The API request could also come from a remote mobile app, so there is no webuser. In that case I don't want to save the user password etc in the app for safety reasons. But could I build it something like this:

- Let the user sign into the app, then call an authentication api to recieve the userguid based on a check against mp_users and the roles tables
- Then save that userguid on the app for e.g. 48 hours
- Then build an IsAllowed function that receives a valid userguid, checks roles etc and returns data based on allowed access for the role

Or can you guide me in a better direction?

Best regards

Lars

6/22/2015 4:24:41 AM
Gravatar
Total Posts 137
When enough isn't

Re: Web API and OData v4

Hi again,

I'm considering to use this approach: http://jasonwatmore.com/post/2014/12/01/Web-API-2-Basic-HTTP-Authentication-Example.aspx

Here I will replace the examples' authorization with a password check against the mojoportal database using the functionality of the 
mojoMembershipProvider and possibly the mojoRoleProvider as well.

Using the approach I should be able to decorate my api controllers with [Authorize] including roles/specific users etc. In that way it should be possible to separate the Authorization process from the Authorize on each controller...

Do you see any conflicts or problems using this approach with MojoPortal and the way security is handled?

Best regards
Lars

You must sign in to post in the forums. This thread is closed to new posts.