【问题标题】:How to resolve System.InvalidOperationException using MVC 6 - Same template如何使用 MVC 6 解决 System.InvalidOperationException - 相同的模板
【发布时间】:2015-05-26 13:36:41
【问题描述】:

我是 ASP.NET 的新手,最近遇到了这个我在网络上找不到的错误(可能是因为最近发布了 MVC 6?)

System.InvalidOperationException The following errors occurred with attribute routing information:

错误 1:

具有相同名称'GetByIdRoute'的属性路由必须具有相同的 模板:操作:'Appname.Web.Controllers.MemberController.GetById' - 模板:'api/Member/{id:int}' 操作: 'Appname.Web.Controllers.PaymentController.GetById' - 模板: 'api/Payment/{id:int}' 操作: 'Appname.Web.Controllers.PlanController.GetById' - 模板: 'api/Plan/{id:int}'

这为startup.cs提供了一个代码剪辑器

Line 73:  
Line 74:              // Add MVC to the request pipeline.
Line 75:              app.UseMvc(routes =>
Line 76:              {
Line 77:                  routes.MapRoute(

第 75 行突出显示

还有这个:

at Microsoft.AspNet.Mvc.ControllerActionDescriptorBuilder.Build(ApplicationModel application) 
at Microsoft.AspNet.Mvc.Core.ControllerActionDescriptorProvider.GetDescriptors() 
at Microsoft.AspNet.Mvc.Core.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context) 
at Microsoft.AspNet.Mvc.Core.DefaultActionDescriptorsCollectionProvider.GetCollection() 
at Microsoft.AspNet.Mvc.Core.DefaultActionDescriptorsCollectionProvider.get_ActionDescriptors() 
at Microsoft.AspNet.Mvc.Routing.AttributeRoute.GetInnerRoute() 
at Microsoft.AspNet.Mvc.Routing.AttributeRoute..ctor(IRouter target, IActionDescriptorsCollectionProvider actionDescriptorsCollectionProvider, IInlineConstraintResolver constraintResolver, ILoggerFactory loggerFactory) 
at Microsoft.AspNet.Mvc.Routing.AttributeRouting.CreateAttributeMegaRoute(IRouter target, IServiceProvider services) 
at Microsoft.AspNet.Builder.BuilderExtensions.UseMvc(IApplicationBuilder app, Action<IRouteBuilder> configureRoutes) 
at Appname.Web.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) in ... Startup.cs:line 75

【问题讨论】:

  • 你不能有不同的路由同名...想象一下当通过路由名称生成链接时,如果有多个同名的路由,应该选择哪一个?...尝试不同GetPaymentByIdGetPlanById 等名称。
  • 正如@KiranChalla 所说,您的 MapRoutes 具有相同的模板名称。如果您提供完整的 app.UseMvc 语句(从第 75 行到路由声明的末尾),肯定有人会准确地告诉您哪些部分需要更改。
  • @Daniel:我面临着类似的问题。您对此有何解决方案?

标签: c# asp.net asp.net-mvc asp.net-core-mvc


【解决方案1】:

您可以指定路线名称。

在 Web API 中,每个路由都有一个名称。路由名称对于生成链接很有用,因此您可以在 HTTP 响应中包含链接。

http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-names

    [HttpGet("{id}", Name = "GetPerson")]
    public IActionResult Get(int id)
    {
        var item = this.PeopleRepository.GetById(id);

        if (item == null)
        {
            return this.HttpNotFound();
        }

        return new ObjectResult(item);
    }

通过这样做,您可以拥有另一个具有相同操作名称(但不同路由名称)的控制器

    [HttpGet("{id}", Name = "GetPurchase")]
    public IActionResult Get(int id)
    {
        var item = this.PurchaseRepository.GetById(id);

        if (item == null)
        {
            return this.HttpNotFound();
        }

        return new ObjectResult(item);
    }

【讨论】:

    【解决方案2】:

    在这种情况下我要做的是

        [HttpGet("{id}", Name = nameof(GetPurchaseById))]
        public IActionResult GetPurchaseById(int id)
        {
            var item = this.PurchaseRepository.GetById(id);
    
            if (item == null)
            {
                return this.HttpNotFound();
            }
    
            return new ObjectResult(item);
        }
    

    这样我就可以在 HttpPost 响应中做

    
        return CreatedAtRoute(nameof(GetPurchaseById), new { Id = res.PurchaseId}, res);
    
    

    这将在您的响应中自动设置 Location 标头以获取创建的实体

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 2014-10-24
      • 2017-04-15
      • 1970-01-01
      • 2018-02-15
      • 2015-04-26
      • 2021-04-25
      相关资源
      最近更新 更多