Razor page and Web API in one web application
This post shows how to use shared projects or shared assemblies for ASP.NET Core API Controllers or ASP.NET Core Razor Pages. Sometimes shared logic for different ASP.NET Web API or Web App projects can be implemented in a shared project. The shared project controllers, Razor Pages, services can be referenced and used in the host web application
In this article I will explain with an example, how to Add API Controller in same Solution (Project) in ASP.Net Core Razor pages.
1. Create new Razor Page project
Goto Visual Studio 2019 and select "ASP.NET Core Web App" -> Select Target Framework 5.0
now new project has been created
Now you can create Razor pages under Pages directory, by default index will be the home page. you can redirect to any page you wish from Startup.cs.
2. Add API Controller (Web API)
Now let's begin with adding a Web API Controller.
It is good practices to keep all the API controllers under folder directory. In order to create new folder, Right click on the project and add new folder called "API".
Now right click on the API folder and add new Controller, here select the API Controller - Empty and name it like "UserController.cs" and click "Add". once done you will see the following class created under "API" directory.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace demoapp.API
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
}
}
You have successfully added a Web API controller to your application.
Now we have to configure the Startup.cs to run the application with two endpoint.
So, goto Startup.cs file and add the following in the configure method
ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddMvc(); //// Added to enable MVC controller
}
Configure app
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()); // add this line of code to enable Cors to your Web API
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers(); // this line allow Web API controller endpoints
});
}
}
Now write simple API Get Method in added API controller
using demoapp.Model;
using Microsoft.AspNetCore.Mvc;
namespace demoapp.API
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
public IActionResult Get()
{
var result = new Users()
{
Name = "Demo User",
Age = "21",
Dob = "Jan 01, 2000"
};
return new JsonResult(result);
}
}
}
Now try Razor page by running the Web App
Now try Web API from Post man
Wow, it's working!