.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 "名字";
    }
}
相关推荐
唐青枫6 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫7 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
Caco_D7 天前
一行代码抓遍全网 20 个热榜!Aneiang.Pa 4.0 发布 — 极简 .NET 爬虫库
爬虫·.net
咕白m6257 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
小码编匠8 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫10 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
网络研究院13 天前
2026年网络安全
网络·安全·法律·法规·趋势·发展
酣大智13 天前
ARP代理--工作原理
运维·网络·arp·arp代理
treesforest13 天前
AI安全系统如何识别异常访问?IP风险识别正在成为关键能力
网络·人工智能·tcp/ip·安全·web安全