2017-02-10 16 views
0

ASP.NET Core 1.1で従来のルーティングを使用するWeb APIがあります。私は含む別のInventoryController.csクラスを持っているWeb APIが従来のルーティングを使用していません

app.UseMvc(routes => 
{ 
    routes.MapRoute(
     name: "api", 
     template: "api/inventory", 
     defaults: new { controller = "Inventory" }); 
}); 

:私は私のStartup.csConfigure方法で、次のコードを持って

public class InventoryController : Controller 
{ 
    [HttpGet] 
    public IEnumerable<string> Get() 
    { 
     return new string[] { "value1", "value2" }; 
    } 
} 

私はapi/inventoryを呼び出すときに、私は["value1","value2"]を受け取ることを期待して、それではありませんケース。私は代わりに404を受け取る。私はASP.NETに慣れていないし、私が考えることができるほとんど全てを試してきました。ここではRouteAttributeを使用するとすべて正常に動作することに注意してください。

答えて

0

は、あなたがこの

app.UseMvc(config => { 
      config.MapRoute(
       name: "Default", 
       template: "{controller}/{action}/{id?}" 
       ,defaults: new { controller="App", action="Index"} 
       ); 
     }); 

ようにそれをやってみました、私は `RouteAttribute`は、すべてが正常に動作します使用して、私の質問で述べたように、あなたのクラスは

[Route("api/Inventory")] 
public class InventoryController : Controller 
{ 
    [HttpGet] 
    public IEnumerable<string> Get() 
    { 
    return new string[] { "value1", "value2" }; 
    } 
} 
+0

のようになります持っています。しかし、なぜ私は従来のルーティングを使用して動作しない知ってほしいです。 – Hele

関連する問題