.Net中路由设置

.Net中路由设置

自定义路由模板

第一种写法

csharp 复制代码
[Route("/api/[controller]/[action]")]
[ApiController]
public class TestController : Controller
{
    [HttpGet("")]
    public string GetRoute1()
    {
        return "使用/api/[controller]/[action],返回路由";
    
}

第二种写法

csharp 复制代码
[Route("/api/[controller]")]
[ApiController]
public class TestController : Controller
{
    [HttpGet("[action]")]
    public string GetRoute2()
    {
        return "使用/api/[controller],返回路由";
    }
}

路径传参

必传参数

其中id可以自定义设置

csharp 复制代码
[HttpGet("/arg/{id}")]
public string GetRouteByArg(int id)
{
    return "路径传参";
}

这样写可以指定id类型

csharp 复制代码
[HttpGet("/arg/{id:int}")]
public string GetRouteByArg(int id)
{
    return "路径传参";
}

可选传参

需要注意的是,这个?要在下面函数中也要指定?

csharp 复制代码
[HttpGet("/choose/arg/{id:int?}")]
public string GetRouteByArgChoose(int? id)
{
    return $"可选择传参{id}";
}

给定默认值传参

csharp 复制代码
[HttpGet("/default/arg/{id:int=200}")]
public string GetRouteByDefaultArg(int id)
{
    return $"给定默认值传参{id}";
}

最大值约束

csharp 复制代码
[ApiController]
[Route("/api/[controller]")]
public class GeneratorController : ControllerBase
{
    [HttpPost("arg/{id:int:max(10)}")]
    public string GetByArgId(int id)
    {
        return $"最大值约束{id}";
    }
}

自定义路由属性模板

重写父类Attribute, IRouteTemplateProvider

我们每次使用[Route]这个是框架封装好的,但是我们有时需要自己自定义。

新建BunnyRoteAttribute.cs文件,在Template中写上你自己的名字即可。

csharp 复制代码
using Microsoft.AspNetCore.Mvc.Routing;

namespace Bunny.LinkGenerator.Net.Config;

public class BunnyRoteAttribute : Attribute, IRouteTemplateProvider
{
    public string? Template => "bunny/myTemplate";
    public int? Order => 1;
    public string? Name { get; set; }
}

也可以指定控制器

csharp 复制代码
public class BunnyRoteAttribute : Attribute, IRouteTemplateProvider
{
    public string? Template => "bunny/myTemplate/[controller]";
    public int? Order => 1;
    public string? Name { get; set; }
}

项目使用

csharp 复制代码
using Bunny.LinkGenerator.Net.Config;
using Microsoft.AspNetCore.Mvc;

namespace Bunny.LinkGenerator.Net.Controller;

[BunnyRote(Name = "CustomRoute")]
[ApiController]
public class CustomController : ControllerBase
{
    [HttpGet("GetByCustom")]
    public string GetByCustom()
    {
        return "自定义属性路由";
    }
}

注意事项

有相同路径

如果路径有相同,但是只要一个路由那么会自动匹配第一个,如果有两个及以上路径相同会报错!!!

csharp 复制代码
[Route("/api/[controller]")]
public class SameNameController : ControllerBase
{
    [HttpGet("GetNameById")]
    public string GetNameById()
    {
        return "134";
    }
    
    [HttpGet("GetNameById")]
    public string GetNameById1()
    {
        return "134";
    }
}

控制器没有使用模板

csharp 复制代码
[ApiController]
public class Warning1Controller : ControllerBase
{
    [HttpGet("GetName/{id:int}")]
    public string GetName(int id)
    {
        return "名字";
    }
}
```csharp
[ApiController]
public class Warning1Controller : ControllerBase
{
    [HttpGet("GetName/{id:int}")]
    public string GetName(int id)
    {
        return "名字";
    }
}
相关推荐
xiaohaiAIgeo21 小时前
【2026年】医药研发实验室GLP的通风设计要求:稳定性可验证性与合规性
网络·人工智能·科普知识
翼龙云_cloud1 天前
亚马逊云渠道代理商:VPC 教程 利用 EC2 自建 NAT 实例降低私有子网出网成本
网络·云计算·aws
xiaoye-duck1 天前
《Linux网络编程》UDP Socket 编程实战:从 Echo 回显、通用字典服务到套接字封装全流程
linux·网络·udp
酒神dnspup1 天前
CDN 回源检测怎么做?用 DNSPup 验证节点命中、源站暴露与多地可用性
运维·服务器·网络·网络协议·tcp/ip
江南风月1 天前
3分钟快速上手日志审计系统 WGLOG v2.2 更新了什么
运维·服务器·网络
Dawn-bit1 天前
Linux网络进阶:TCP三次握手和四次挥手与TCP连接状态
linux·服务器·网络·tcp/ip·云计算·智能路由器
TheBestRucy1 天前
Python 九阳神功:从零筑基到线程飞升
服务器·开发语言·网络·人工智能·python
酒神dnspup1 天前
Tcping 端口连通性检测教程:用 DNSPup 判断防火墙、监听与线路故障
网络·网络协议·tcp/ip·ip·dnspup
xier_ran1 天前
【infra之路】GPU 存储层次总结:L1 / L2 / HBM
java·网络·数据库
小网洞1 天前
免费WPA无线握手包在线分析跑包网站
网络·网络安全·密码学·wps