2016-11-19 21 views

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

.

https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs.

In einer Erweiterungsmethode von UseMvc Sie folgende Zeile finden.

routes.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(app.ApplicationServices));