Vivek
Vivek

Reputation: 93

Rest API ASP.NET

I have created an MVC project (Say DeploymentTool) and added another webAPI project to the solution (Say DeploymentToolAPI), and hence NameSpace is different for both. Now what I want is, to call any HTTPPOST/ HTTPGET method of DeploymentToolAPI from outside, say POSTMAN or web browser. Do we need to update WebApiConfig.cs for that? If not, how can I find the URL of DeploymentToolAPI. I am able to call any method of DeploymentTool but it's not working in case of DeploymentToolAPI.

Would appreciate any help/ suggestions.

Upvotes: 1

Views: 202

Answers (2)

Denys Wessels
Denys Wessels

Reputation: 17039

Firstly you need to Set Multiple Startup Projects to make both your API service and the MVC web application start up simultaneously:

In Visual Studio right click your solution -> Properties -> Select "Multiple startup projects:" -> set the Action of both projects to Start:

Set Multiple Startup Projects in Visual Studio

Once this is done follow the instructions posted by @Ankush Jain or just copy the WEB API url from the browser window, because when you run the project after setting multiple startup projects you will see that both your web api service and the MVC web application will be opened in the browser in two different tabs.

Upvotes: 1

Ankush Jain
Ankush Jain

Reputation: 7049

  1. Right click on your WEB API project and go to Properties
  2. Go on Web tab and you will see project URL textbox
  3. This will tell you the base url for your web api project. (ie. http://localhost:8526)
  4. Then go to WebApiConfig.cs to check URL route pattern.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;
    
    namespace StandardWebApiTemplateProject
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }
        }
    }
    

If you have Route Template like -

routeTemplate: "api/{controller}/{id}" then your url will be like 
ie. http://localhost:8526/api/ControllerName/89

and if you include action name in route template then case will be like below

routeTemplate: "api/{controller}/{action}/{id}" then your url will be like 
ie. http://localhost:8526/api/ControllerName/ActionName/89

Upvotes: 2

Related Questions