1.2K Star 6.6K Fork 2.6K

zuohuaijun / Admin.NET

 / 详情

Furion从4.6.7升级到4.8.8.14出现报错Microsoft.AspNetCore.Routing.Patterns.RoutePatternException: The route parameter name 'id' appears more than one time in the route template.

已完成
创建于  
2023-05-15 14:59

Furion 版本号

项目版本是Furion.Pure 4.6.7


.NET SDK 版本号

  • .NET5
  • .NET6
  • .NET7

项目类型

  • WebApi
  • Mvc
  • Razor Pages
  • Blazor Server
  • MinApp
  • WinForm
  • WPF
  • Console

操作系统和版本

  • Windows(版本号:Windows 10 专业版)
  • Linux(版本号?)
  • MacOS(版本号?)
  • 其他(版本号?)

代码环境

  • 开发环境(Development)
  • 生产环境(Production)
  • 测试环境(Tests/单元测试/集成测试 )

描述你的问题

将项目版本Furion.Oure 4.6.7,升级到版本 4.8.14后再启动项目就发生报错:
Microsoft.AspNetCore.Routing.Patterns.RoutePatternException: The route parameter name 'id' appears more than one time in the route template.

异常堆栈信息

at Microsoft.AspNetCore.Routing.Patterns.RoutePatternParser.Parse(String pattern)
at Microsoft.AspNetCore.Routing.Patterns.RoutePatternFactory.Parse(String pattern)
at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointFactory.AddEndpoints(List1 endpoints, HashSet1 routeNames, ActionDescriptor action, IReadOnlyList1 routes, IReadOnlyList1 conventions, Boolean createInertEndpoints)
at Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource.CreateEndpoints(IReadOnlyList1 actions, IReadOnlyList1 conventions)
at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.UpdateEndpoints()
at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Initialize()
at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.get_Endpoints()
at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.<>c.b__15_0(EndpointDataSource d)
at System.Linq.Enumerable.SelectManySingleSelectorIterator2.ToArray() at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)
at Microsoft.AspNetCore.Routing.CompositeEndpointDataSource.Initialize()
at Microsoft.AspNetCore.Routing.DataSourceDependentCache1.Initialize() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory)
at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory) at Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher..ctor(EndpointDataSource dataSource, Lifetime lifetime, Func1 matcherBuilderFactory)
at Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource dataSource)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.g__AwaitMatcher|8_0(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task`1 matcherTask)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Furion.UnifyResult.UnifyResultStatusCodesMiddleware.InvokeAsync(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request.


测试项目代码

⚠⚠ 必须提供完整可运行且包含错误的 Git 仓库 DEMO,DEMO 提供最简单的错误逻辑代码,否则将无法得到答复。⚠⚠

您的代码下载地址?
我使用的是Admin.NET项目,项目地址: https://gitee.com/zuohuaijun/Admin.NET.git


数据库信息和版本

请提供安装的 NuGet 包版本号。

  • Sqlite(包名和版本号?)
  • SqlServer(包名和版本号?)
  • MySQL(包名和版本号 8.0.22)
  • Oracle(包名和版本号?)
  • PostgreSQL(包名和版本号?)
  • Firebird(包名和版本号?)
  • Cosmos(包名和版本号?)
  • InMemoryDatabase(包名和版本号?)

期待结果

期待的结果是?

项目版本在Furion.Pure 4.6.7中可以使用,期望Furion.Pure 升级到版本 4.8.14,并能运行不报错。

感谢大神无私奉献,好人一生平安!!!

评论 (14)

Boda_Smart 创建了任务
Boda_Smart 修改了描述
Boda_Smart 修改了描述
展开全部操作日志

升级到 v4.8.8.15 版本,这个从早上7点就处理了,升级一下再反馈吧。。。

您好,我试过了,还是会报错,所以提交到这里:
Furion版本
输入图片说明

报错stack
输入图片说明

切换到项目原始版本,启动就不会报错。
输入图片说明

那只能等你提供一个复现错误的 demo 给我了,别把完整的项目发上来。

这个错误只有一种可能,那就是自定义路由冲突了,出现了多个参数。

我用 Admin.NET 最新版测试正常,你把 Application 的动态 API 类一个一个的删,删一个如果还有问题就继续删,删到最后一个正常的类文件,那么就是这个文件出问题了,到时候把这个文件发上来。

输入图片说明

找到问题的服务了,感谢指导!

代码如下:

  1. 仓储接口
public interface IBasicRepository<TAddEntity, TEditEntity>
        where TAddEntity : class
        where TEditEntity : class
{
    Task<JsonResult> GetAllAsync();
    Task<JsonResult> GetByIdAsync(long id);
    Task<JsonResult> AddAsync(TAddEntity dto);
    Task<JsonResult> EditAsync(TEditEntity dto);
    Task<JsonResult> DelAsync(long id);

    Task<JsonResult> QueryByPage(int page, int pageSize, string field, bool isDesc);
}
  1. 仓储接口实现
public class BaseService<TAddEntity, TEditEntity, Entity> : IBasicRepository<TAddEntity, TEditEntity>
        where TAddEntity : class
        where TEditEntity : EntityBase
        where Entity : EntityBase
{
    private readonly ILogger<BaseService<TAddEntity, TEditEntity, Entity>> logger;
    private readonly IRepository<Entity> _repository;

    public BaseService(ILogger<BaseService<TAddEntity, TEditEntity, Entity>> logger, IRepository<Entity> repository)
    {
        this.logger = logger;
        _repository = repository;

        logger.LogInformation("BaseService init");
    }

    [HttpPost]
    public virtual async Task<JsonResult> AddAsync(TAddEntity dto)
    {
        var entity = dto.Adapt<Entity>();
        var res = await _repository.InsertAsync(entity);
        return new JsonResult(res);
    }

    [HttpGet]
    public virtual async Task<JsonResult> DelAsync(long id)
    {
        var res = await _repository.DeleteAsync(id);
        return new JsonResult(res);
    }

    [HttpPost]
    public virtual async Task<JsonResult> EditAsync(TEditEntity dto)
    {
        // 首先找出源
        var dbEntity = await _repository.FindByKeyAsync(dto.Id);
        if (dbEntity is null)
        {
            throw new Exception("We can't find the entity you want to updated from reposiroty!");
        }
        dbEntity = dto.Adapt<Entity>();
        var res = await _repository.UpdateAsync(dbEntity);
        return new JsonResult(res);
    }

    [HttpGet]
    public virtual async Task<JsonResult> GetAllAsync()
    {
        var res = await _repository.GetListAsync();
        return new JsonResult(res);
    }

    [HttpGet]
    public virtual async Task<JsonResult> GetByIdAsync(long id)
    {
        var res = await _repository.FindByKeyAsync(id);
        return new JsonResult(res);
    }

    [HttpGet]
    public virtual async Task<JsonResult> QueryByPage([FromQuery] int page, [FromQuery] int pageSize, [FromQuery] string field, [FromQuery] bool order)
    {
        var res = await _repository.GetPagedInfoListAsync(page, pageSize, field, order);
        return new JsonResult(res);
    }

    /// <summary>
    /// Route("{id:int}") 可以形成唯一的url,带有参数id约束为int
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [AllowAnonymous]
    [Route("{id:int}")]
    [HttpGet]
    public JsonResult Hello([FromRoute] int id)
    {
        return new JsonResult($"hello world id is {id}!");
    }
}
  1. 服务
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 2000)]
public class DeviceCodeService : BaseService<DeviceAddDto, Device, Device>, IBasicRepository<DeviceAddDto, Device>, IDynamicApiController, ITransient
{
    public DeviceCodeService(ILogger<BaseService<DeviceAddDto, Device, Device>> logger, IRepository<Device> repository) : base(logger, repository)
    {
    }
}

去掉这个DeviceCodeService的服务就好了

那就是这个问题了,我来测试。

输入图片说明

实际上你这个是多此一举,不用贴 [Route("{id:int}")]...删除即可。

果然是它:输入图片说明


这个实际上是你错误的使用路由导致的,但是看起来又那么的合理。。。所以我处理一下吧。。。

注释掉下面的代码就好了 :sweat: ,应该是多了特性或者方法中的Route,旧版本不报错,新版本会报错。 感谢!

 /// <summary>
    /// Route("{id:int}") 可以形成唯一的url,带有参数id约束为int
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [AllowAnonymous]
    [Route("{id:int}")]
    [HttpGet]
    public JsonResult Hello([FromRoute] int id)
    {
        return new JsonResult($"hello world id is {id}!");
    }

旧版本不报错才是 bug....

已修复(实际上不是问题,应该说已支持...):https://gitee.com/dotnetchina/Furion/commit/6c6434c37124f62d64ca2371d89537273a36abff

下个版本(明天)发布。

越来越变态了,你们花样真多...,不是一般人都不敢支持...

:smile: 因为之前都没有报错过,我只是测试Hello不同路由的方法,后面也没删除,留在里面了,也不报错,所以没向这里去想。独此一份的bug哈哈。

:+1: Furion是越来越好用了,配合Admin.Net,确实省事不少,很强大,不管是学习还是公司用,都不错。
后面会一直支持,把.Net做大做强,再创辉煌。

已发布最新版 v4.8.8.18,已支持。

Boda_Smart 任务状态待办的 修改为已完成

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
974299 monksoul 1578937227
C#
1
https://gitee.com/zuohuaijun/Admin.NET.git
git@gitee.com:zuohuaijun/Admin.NET.git
zuohuaijun
Admin.NET
Admin.NET

搜索帮助

53164aa7 5694891 3bd8fe86 5694891