Fred
Fred

Reputation: 4076

How to use razor pages and controllers in the same .net core 3 app?

Here's the code for configuration in startup

public void ConfigureServices(IServiceCollection services) {
  services.AddControllers();
  services.AddRazorPages();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
    app.UseEndpoints(endpoints => {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
    });
}

I would like to have all of the controllers mapped under '/api' and everything else maps to razor pages. I've done a ton of searching the web, but I can't seem to find what I'm looking for.

Upvotes: 7

Views: 8666

Answers (2)

Ryan
Ryan

Reputation: 20116

There's no problem with your startup.cs.If you want to map all /api to your controller, then just decorate your api controller with attribute routing like:

[ApiController]
[Route("/api/[controller]")]
public class WeatherForecastController : ControllerBase

Then create a Pages folder in your project where locates Razor Pages. enter image description here

Upvotes: 9

hmiedema9
hmiedema9

Reputation: 988

Have you seen this yet?

http://www.binaryintellect.net/articles/e6557104-d06a-418c-a1a9-b8ce248f60b1.aspx

Looks like it can be done. You just won't call services.AddRazorPages();

Let me know what you see wrong with this or if it works.

Upvotes: 0

Related Questions