【问题标题】:How to add controller endpoints to Razor Server in ASP.NET Core 7.0 / VS2022如何在 ASP.NET Core 7.0 / VS2022 中将控制器端点添加到 Razor Server
【发布时间】:2023-01-31 13:22:31
【问题描述】:

我正在尝试添加一个路由到 Razor 服务器端应用程序的控制器。我尝试了几件事,但我只看到 .NET 6 的解决方案并且无法弄清楚。

我创建了一个这样的控制器:

using Microsoft.AspNetCore.Mvc;

namespace SAAR.Controllers
{
    [ApiController]
    [Route("settings/[controller]")]
    public class ConfigurationController : ControllerBase
    {
        public ConfigurationController()
        {
        }

        [HttpGet("test")]
        public string Test()
        {
            return "Test a controller in Razor";
        }
    }
}

然后在我的program.cs 中添加:

builder.Services.AddControllers();

app.UseEndpoints(endpoints => {
    endpoints.MapRazorPages();
    endpoints.MapControllers();
});

AFAIK 这应该可以工作并且端点 http://localhost:5000/settings/test 应该在那里,但我收到 http 404 错误。

我究竟做错了什么?

这是我完整的program.cs

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.EntityFrameworkCore;
using SAAR.Areas.Identity;
using SAAR.Data;
using SAAR.Services;
using SAAR.Settings;
using System.Configuration;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));

builder.Services.AddDatabaseDeveloperPageExceptionFilter();

// Add MVC Controllers
builder.Services.AddControllers();
builder.Services.AddRazorPages();

builder.Services.AddServerSideBlazor();
builder.Services.AddTransient<IEmailSender, EmailSender>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
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.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.UseEndpoints(endpoints => {
    endpoints.MapRazorPages();
    endpoints.MapControllers();
});

app.Run();

谢谢你的帮助...

【问题讨论】:

  • 尝试使用 http://localhost:5000/settings/configuration/test - 你在控制器的“基本”URL 中有 [Controller] 部分 - 所以它也会显示在你的 URL 中

标签: c# asp.net-core-mvc razor-pages asp.net-core-7.0


【解决方案1】:

在你的情况下,uri 应该是http://localhost:5000/settings/Configuration

您的控制器的名称是 Configuration 而不是 test

[ApiController]
[Route("settings/[controller]")]
public class ConfigurationController : ControllerBase
{
 .....
}

【讨论】:

    【解决方案2】:

    好的,明白了,谢谢!

    配置控制器.cs 路线:/配置/测试

    using Microsoft.AspNetCore.Mvc;
    
    namespace SAAR.Controllers
    {
        [ApiController]
        [Route("[controller]")]
        public class ConfigurationController : ControllerBase
        {
            public ConfigurationController()
            {
            }
    
            [HttpGet("[action]")]
            public string Test()
            {
                return "Test a controller in Razor";
            }
        }
    }
    

    在 program.cs 中添加此代码并删除重复的 app.[Method] 调用。

    app.UseEndpoints(endpoints => {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
    
    });
    

    并将其插入 program.cs

    builder.Services.AddControllers();
    

    所以完整的 Program.cs 是

    using Microsoft.AspNetCore.Components;
    using Microsoft.AspNetCore.Components.Web;
    using Microsoft.EntityFrameworkCore;
    using SAAR.Areas.Identity;
    using SAAR.Data;
    using SAAR.Services;
    using SAAR.Settings;
    using System.Configuration;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
    builder.Services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(connectionString));
    builder.Services.AddDatabaseDeveloperPageExceptionFilter();
    
    // Add MVC Controllers
    builder.Services.AddControllers();
    builder.Services.AddRazorPages();
    
    builder.Services.AddServerSideBlazor();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseMigrationsEndPoint();
    }
    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.UseEndpoints(endpoints => {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
    
    });
    app.Run();
    

    【讨论】:

      【解决方案3】:

      您不能在 Razor Server Web 应用程序中使用路由/MVC 控制器。

      【讨论】:

      • 这是不正确的。您可以在同一个应用程序中混合搭配 Razor Pages 和 MVC/API 控制器。
      猜你喜欢
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多