I have created a new asp.net (standard) web api project and now I would like to have the front logic (ASP.net MVC) in the same project, I came to know that we can have both client (MVC) and server (Web API) application in the same project. I tried to find the step by step process to follow, but I couldn't find any. I would really appreciate if someone can provide me steps to follow, as this is the first time I'm building a application from scratch. Thanks in advance.
Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET.
First of all, create MVC controller class called StudentController in the Controllers folder as shown below. Right click on the Controllers folder > Add.. > select Controller.. Step 2: We need to access Web API in the Index() action method using HttpClient as shown below.
Although ASP.NET Web API is packaged with ASP.NET MVC, it is easy to add Web API to a traditional ASP.NET Web Forms application. To use Web API in a Web Forms application, there are two main steps: Add a Web API controller that derives from the ApiController class. Add a route table to the Application_Start method.
You could try to create an empty asp.net project and define that this project has the web and api features. For sample:
Empty
and check the options, MVC
and Web Api
In the App_Start
folder you will see two files. The RouteConfig.cs
file define the routes for the ASP.NET MVC Web App. The WebApiConfig.cs
file define the routes for the ASP.NET Web Api application.
Importante: The API controllers has a prefix on the route called api
, so, when you create a controller that inherits from ApiController
, it will be routed by api/ControllerName
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With