2017-08-21 47 views

回答

1

在控制器的頂部使用Route屬性將允許您在整個控制器上定義路徑。 [Route("anothernamethatpointstohomeactually")]

您可以閱讀更多here

1

您可以app.UseMvc(routes =>塊內的Startup.Configure方法添加新Routes

routes.MapRoute(
       name: "SomeDescriptiveName",      
       template: "AnotherNameThatPointsToHome/{action=Index}/{id?}", 
       defaults: new { controller = "Home"} 
      ); 

的代碼非常類似於ASP.NET MVC。

欲瞭解更多信息,請參閱Routing in ASP.NET Core

下面是ASP.NET MVC(ASP.NET沒有核心MVC)

您還可以通過routes.MapRouteRouteConfig添加新Route

routes.MapRoute(
      name: "SomeDescriptiveName", 
      url: "AnotherNameThatPointsToHome/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 

確保,你插入在您定義您的Default路線之前的代碼。

欲瞭解更多信息,請訪問docs

5

我建議你使用屬性的路由,當然這取決於您的方案。

[Route("prefix")] 
public class Home : Controller { 

    [HttpGet("name")] 
    public IActionResult Index() { 
    } 

} 

這將在url.com/prefix/name

發現有很多的選擇,屬性的路由,一些樣品:

[Route("[controller]")] // there are placeholders for common patterns 
          as [area], [controller], [action], etc. 

[HttpGet("")] // empty is valid. url.com/prefix 

[Route("")] // empty is valid. url.com/name 

[HttpGet("/otherprefix/name")] // starting with/won't use the route prefix 

[HttpGet("name/{id}")] 
public IActionResult Index(int id){ ... // id will bind from route param. 

[HttpGet("{id:int:required}")] // you can add some simple matching rules too. 

檢查Attribute Routing official docs