2016-11-19 12 views

答えて

2

短い答え:属性ベースのルートが優先されます。

例:

ここでは何も説明できません。私はapp.UseMvc

コントローラは、このように見えるで構成されたものをここで

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

今、あなたは、私はそれが条約に基づくルートの を提示されていないIdが大きいか20と同じ構成に等しいの値を除く外なければならないことを属性ルートでの制約が適用されていることを見ることができます。ブラウザで今

(属性ベースとして、ルートは最初に追加した理由この例では、それは

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. 

今、あなたは、他のルートはそれほど追加知っているだろう。

Githubのasp.netのコアソースで以下のファイルを参照してください。

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

次の行を見つけますUseMvcの拡張方法の一つで。

routes.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(app.ApplicationServices)); 
関連する問題