.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 "名字";
    }
}
相关推荐
赵榕18 分钟前
ClaimsPrincipal序列化为Json的正确姿势
.net
追逐时光者11 小时前
一款使用 C# 编写专为 Windows 11 打造的文件资源管理器增强工具!
后端·.net
用户298698530143 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
牧马人win3 天前
SmartDapper.Repository
.net
mudtools4 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的5 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
DianSan_ERP5 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅5 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑