为什么我的 .net Core API 属性路由不起作用?

问题描述 投票:0回答:3

我正在试验“Hosted Blazor WASM”项目。此问题不涉及 Blazor 客户端路由器(工作正常)。这涉及“服务器端”路由器在执行属性路由时无法按预期工作。

这是我的项目文件:

using Microsoft.AspNetCore.ResponseCompression;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseWebAssemblyDebugging();
}
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.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();

app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");

app.Run();

我添加了以下控制器:

[ApiController]
public class PagesController : ControllerBase
{

public PagesController()
{
}

// GET: api/pages
[HttpGet]
[Route("api/{controller}")]
[Route("api/{controller}/{action}")]
[Route("api/{controller}/{action}/{param}")]
public async Task<PageVm> Get(string controller, string? action, string? param, string? WebsiteId)
{
    DomainLogic dl = new DomainLogic();

    PageVm PageVm = new PageVm();

    PageVm.WebsiteId = await dl.GetWebsiteIdFromDomainAsync(db, Request);

    Console.WriteLine($"{controller} Get() was called");
    return PageVm;
}
}

[路线(“api/{controller}”)]<-- this is working with https://localhost:7237/api/pages/

[路线("api/{controller}/{action}")] <-- this is NOT working https://localhost:7237/api/pages/something

后一条路径根本无法识别,因为我们被路由到 index.html 这是后备。

提前致谢!

asp.net-core asp.net-web-api2 asp.net-web-api-routing
3个回答
0
投票
路由中的

{controller}
{action}
是保留关键字。按照惯例,当您在路由中使用
{controller}
时,它表示控制器名称,而当您使用
{action}
时,它表示方法名称。所以在你的情况下
"api/{controller}/{action}"
的路线是
api/pages/get
因为控制器名称是
PagesController
并且方法名称是
Get
.


0
投票

您可以添加

[HttpGet("/api/pages/something")]
使“https://localhost:7237/api/pages/something”在方法上工作,就像:

    [ApiController]
    public class PagesController : ControllerBase
    {
        public PagesController()
        {
        }

        // GET: api/pages
        [HttpGet("/api/pages/something")]
        public async Task<PageVm> Get(string controller, string? action, string? param, string? WebsiteId)
        {
            DomainLogic dl = new DomainLogic();

            PageVm PageVm = new PageVm();

            PageVm.WebsiteId = await dl.GetWebsiteIdFromDomainAsync(db, Request);

            Console.WriteLine($"{controller} Get() was called");
            return PageVm;
        }
    }

0
投票

你有一个错误的语法,应该是

[HttpGet]
[Route("~/api/[controller]")]
[Route("~/api/[controller]/[action]")]
[Route("~/api/[controller]/[action]/{param}")]
[Route("~/api/[controller]/[action]/{param}/{WebsiteId}")]
public async Task<PageVm> Get(string? param, string? WebsiteId)
{
}
        
© www.soinside.com 2019 - 2024. All rights reserved.