[英]Issue with area route in Asp.Net Core 2.2
在我的應用程序內部,我的路線似乎有問題。 我無法確定路由為什么會失敗。 該解決方案包含一個從[ApiController]
派生的控制器,以遵守Web Api標准。
Area > > > Devices > > > Sampler (Controller)
/api/Devices/Sampler/GetSamples/1
{area:exists}/{controller}/{action}/{id?}
我的想法是,當使用MapAreaRoute
時,第三行模板將相互關聯,代表第一行和第二行。
application.UseMvc(routes =>
{
routes.MapAreaRoute(
name: "Device Route",
areaName: "Devices",
template: "api/{area:exists}/{controller}/{action}/{id?}
);
}
即使我從模板中刪除api
,指定區域,控制器和操作,我仍然會收到404。
[ApiController]
[Area("Devices")]
[Route("api/[controller]")]
public class SamplerController : ControllerBase
{
[HttpGet("{id}")]
public JsonResult GetSamples(Guid id) => new JsonResult("...");
}
有人可以詳細說明為什么該區域不起作用嗎? 如果我創建一個名為Controllers的目錄,則將MapRoute
與上面的相同代碼一起使用,即可按預期工作。
您需要根據docs使用一個屬性來定義此控制器所屬的區域。
[Area("Products")]
public class ManageController : Controller
更新:另外,您還定義了api/{area:exists}/{controller}/{action}/{id?}
作為路由,並根據您的評論嘗試使用api/devices/sampler/8
。 因此,以您的路線為指導,您的網址應為api/devices/sampler/GetSamples/8
。 您需要在網址中包含操作名稱,因為這是您在路由中的布局方式: api/{area:exists}/{controller}/{action}/{id?}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.