Wenn ich beide, Konvention und Attribut-basierte Routing in einer ASP.NET MVC Core-Anwendung. Welche Art von Route hat Vorrang, wenn beide übereinstimmen würden?Precedence Konvention vs vs. attributbasiertes Routing in ASP.NET MVC Core
0
A
Antwort
2
Kurz gesagt: Attributbasierte Route hat Vorrang.
Beispiel:
Wie Sie kein Beispiel hier zur Verfügung gestellt hat, was ich am besten erklären.
Hier ist, was ich in app.UseMvc konfiguriert
-Controller wie folgt aussehen.
[Route("My")] // Disable this line if attribute route need to be disable.
public class MyController : Controller
{
[Route("Index/{id:min(20)}")] // Disable this line if attribute route need to be disable.
// GET: /<controller>/
public IActionResult Index(int id)
{
return Content("This is just test : " + id.ToString());
}
}
Jetzt können Sie sehen, dass ich Einschränkung in Attribut Route angewendet haben, dass es Wert von Id größer oder gleich 20 Gleiche Konfiguration ist für Konvention basiert Route nicht vorhanden ausnehmen müssen.
Jetzt im Browser (in diesem Beispiel wird es
http://localhost:yourport/My/Index/1 // This will match with contention based route but will not call as attribute route will take precendence
http://localhost:yourport/My/Index/21 // This will call successfully.
Jetzt wissen Sie, warum als Attribut basierte Route zuerst dann andere Route so hinzugefügt hinzugefügt.
In Github asp.net Kern Quelle siehe folgende Datei
.In einer Erweiterungsmethode von UseMvc Sie folgende Zeile finden.
routes.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(app.ApplicationServices));
Verwandte Themen
- 1. Asp.Net Core - MVC Vs. WebAPI Routing-Unterschied
- 2. ASP.NET MVC Routing vs bestimmte Aktionen
- 3. ASP.NET MVC: url Routing vs Abfragezeichenfolgeflag
- 4. ASP.Net VS ASP.Net MVC
- 5. ASP.NET MVC vs Webforms
- 6. ASP.NET MVC vs. ASP.NET 4.0
- 7. .NET // vs /// Kommentare Konvention
- 8. ASP.Net vs MVC vs WebAPI und UseTaskFriendlySynchronizationContext
- 9. Asp.net Core EF in VS 2015
- 10. ASP.NET MVC Routing über Methodenattribute GET vs. POST
- 11. ASP.NET AJAX vs jQuery in ASP.NET MVC
- 12. VS für Mac und ASP.NET Core
- 13. Dynamisches Routing in ASP.Net Core
- 14. ASP.NET MVC: ModelState vs. ModelStateDictionary
- 15. Python Django vs ASP.NET MVC
- 16. MVC Routing vs. Autorisierung/Autorisieren Tag
- 17. Asp.Net Mvc: ContentResult vs. Zeichenfolge
- 18. ASP.NET MVC ExecuteResult vs ActionResult
- 19. ASP MVC 5 Attribut-Routing VS. Convention-based Routing
- 20. Angular Routing vs Ionisches Routing
- 21. ASP.NET MVC JavaScript Routing
- 22. Redirect() vs RedirectPermanent() in ASP.NET MVC
- 23. Entsperren asp.net MVC-Vorlagen in VS 2008
- 24. Task.Run vs. ContinueWith in ASP.NET MVC
- 25. ASP.NET MVC und Angularjs vs ASP.NET MVC und Reactjs
- 26. Wie erzwingen Sie Kleinbuchstaben-Routing in ASP.NET Core MVC 6?
- 27. WebAPI 2.2, Attributbasiertes Routing und Abfragezeichenfolgen
- 28. AOP vs MVC FilterAttributes vs Interzeptoren
- 29. MVC 6 .NET-Core vs. .NET Framework 4.6 Leistungsvergleich
- 30. ASP.net MVC Views Modell vs ViewData.Model?