External API access for Plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Hi

I am currently developing a plugin for version 3.8. I am trying to add Web Api to the plugin so it can be reached from any external source and talk directly to the Api Controllers in my plugin.

I have read through your documentation and forums and can not find anything regarding this. It might just be me not using correct search terms. I have referenced Web.Api and can inherit from ApiController. I have specified a route on top of the controller.

However it does seem like I am missing something in the lines off:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

I will be doing my own authentication based on a secret and API key configured in the plugin Configure.

I am not sure. I am a junior developer so I might be overlooking the obvious.  

Any help would be greatly appreciated.
7 years ago
Ok so I have succeeded.

I am 100% sure that this is not the best solution however, it is working for me.

Firstly you need to reference all from Nuget:


Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.webHost

Also make sure you have Nop.Web.Framework.Mvc reference.

After this go ahead and add a class to your Plugin and call it RouteConfig. Inherit from the following IRouteProvider

public class RouteConfig : IRouteProvider
    {
        public int Priority
        {
            get
            {
                return 0;
            }
        }

        public void RegisterRoutes(RouteCollection routes)
        {      

            GlobalConfiguration.Configure(config =>
            {
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            });

        }


You might have this already and yours might contain additional code depending on other MVC routes.


Then you create a Class, you can call it whatever you want, followed by Controller. Mine was Named SyncContorller. this will be used as your API Controller.

Make sure you inherit from ApiController.

public class SyncController : ApiController
{
        [HttpGet]        
        public object Get()
        {            
            throw new NotImplementedException();
        }
}


Make sure you do a proper clean and rebuild then you should be able to hit it once you have ran your solution.

I could hit the Get method by using http://localhost:15536/api/sync . Yours might be different depending on your port number.
5 years ago
Hi,
I am using NOPCommerce 4.0 along with API plugin 4.0 provided by the same link.
After adding the api plugin I am not able to add any controller in Nop.Web and if I add controller from another project it starts to giving a dependency injection exception.

Plugin 'Api plugin'. Could not load file or assembly 'Microsoft.EntityFrameworkCore.Relational, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly

I tried everything from my side but couldn't find any solution,  if I unload the api plugin project it starts working properly.
5 years ago
reier360,

[email protected] wrote:

Firstly you need to reference all from Nuget:


Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.webHost

Also make sure you have Nop.Web.Framework.Mvc reference.


Can you tell me how you added these to your plugin from Nuget?

Thanks,
Tony
3 years ago
[email protected] wrote:
Ok so I have succeeded.

I am 100% sure that this is not the best solution however, it is working for me.

Firstly you need to reference all from Nuget:


Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.webHost

Also make sure you have Nop.Web.Framework.Mvc reference.

After this go ahead and add a class to your Plugin and call it RouteConfig. Inherit from the following IRouteProvider

public class RouteConfig : IRouteProvider
    {
        public int Priority
        {
            get
            {
                return 0;
            }
        }

        public void RegisterRoutes(RouteCollection routes)
        {      

            GlobalConfiguration.Configure(config =>
            {
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            });

        }


You might have this already and yours might contain additional code depending on other MVC routes.


Then you create a Class, you can call it whatever you want, followed by Controller. Mine was Named SyncContorller. this will be used as your API Controller.

Make sure you inherit from ApiController.

public class SyncController : ApiController
{
        [HttpGet]        
        public object Get()
        {            
            throw new NotImplementedException();
        }
}


Make sure you do a proper clean and rebuild then you should be able to hit it once you have ran your solution.

I could hit the Get method by using http://localhost:15536/api/sync . Yours might be different depending on your port number.


Thanks for the Help.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.