簡體   English   中英

如何在ASP.NET Core 7.0 / VS2022中將controller端點添加到Razor服務器

[英]How to add controller endpoints to Razor Server in ASP.NET Core 7.0 / VS2022

我正在嘗試添加一個 controller 並路由到 Razor 服務器端應用程序。 我嘗試了幾件事,但我只看到 .NET 6 的解決方案,無法弄清楚。

我這樣創建了一個 controller:

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();

謝謝你的幫助...

在你的情況下,uri 應該是http://localhost:5000/settings/Configuration

你的controller的名字是Configuration not test

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

好的,明白了,謝謝!

只需創建一個新的 Razor Server 項目並添加一個文件夾“Controllers”並添加一個 C# class:ConfigurationController.cs:

using Microsoft.AspNetCore.Mvc;

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

        [HttpGet("[action]")]
        public string Test()
        {
            // Route: /Configuration/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();

您不能在 Razor 服務器 web 應用程序中使用路由/MVC 控制器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM