如何使用MVC 6解析System.InvalidOperationException - 相同的模板

时间:2015-05-26 13:36:42

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

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

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

错误1:

  

具有相同名称'GetByIdRoute'的属性路由必须具有相同的名称   template:Action:'Appname.Web.Controllers.MemberController.GetById' -   模板:'api / Member / {id:int}'动作:   'Appname.Web.Controllers.PaymentController.GetById' - 模板:   'api / Payment / {id:int}'动作:   'Appname.Web.Controllers.PlanController.GetById' - 模板:   'API /计划/ {ID:整数}'

这为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

2 个答案:

答案 0 :(得分:11)

您可以指定路线名称。

  

在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);
    }

答案 1 :(得分:0)

在这种情况下我做的是

rules: {
  "@typescript-eslint/ban-types": "off"
}

以便在 HttpPost 响应中我可以做

    [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);
    }

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