2015-05-26 35 views
5

Tôi mới dùng ASP.NET nói chung và gần đây đã gặp lỗi này mà tôi không thể tìm thấy trên web (có thể vì bản phát hành gần đây của MVC ? 6)Cách giải quyết System.InvalidOperationException sử dụng MVC 6 - Cùng mẫu

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

Lỗi 1:

Thuộc tính tuyến đường với cùng tên 'GetByIdRoute' phải có cùng mẫu: hành động: 'Appname.Web.Controllers.MemberController.GetById' - Mẫu: 'api/Member/{id: int} 'Hành động: ' Appname.Web.Controllers.PaymentController.GetById '- Mẫu: ' api/Payment/{id: int} 'Hành động: ' Appname.Web.Controllers.PlanController.GetById '- Mẫu: 'api/Kế hoạch/{id: int}'

Điều này cho phép một thợ mã để startup.cs

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

với dòng 75 nhấn mạnh

và điều này:

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 
+1

Bạn không thể có con đường khác nhau có cùng tên .. .imagine khi tạo liên kết theo tên tuyến đường, nếu có nhiều tuyến đường có cùng tên, cái nào nên được chọn? ... hãy thử có các tên khác nhau như 'GetPaymentById',' GetPlanById', v.v. –

+0

Như @KiranChalla nói s, MapRoutes của bạn có cùng tên mẫu. Nếu bạn cung cấp toàn bộ câu lệnh 'app.UseMvc' (bắt đầu dòng 75 đến cuối khai báo tuyến), ai đó chắc chắn sẽ cho bạn thấy chính xác phần nào cần thay đổi. –

+0

@Daniel: Tôi đang đối mặt với một vấn đề tương tự. Giải pháp của bạn cho điều này là gì? –

Trả lời

6

Bạn có thể chỉ định tên tuyến đường.

Trong API web, mỗi tuyến đều có tên. Tên tuyến đường hữu ích cho việc tạo liên kết để bạn có thể đưa liên kết vào phản hồi 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); 
    } 

Bằng cách đó bạn có thể có một bộ điều khiển với tên hành động tương tự (nhưng tên tuyến đường khác nhau)

[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); 
    } 
Các vấn đề liên quan