[英]Issue with area route in Asp.Net Core 2.2
Inside my application I appear to have an issue with my route. 在我的应用程序内部,我的路线似乎有问题。 I cannot determine why the route would be failing.
我无法确定路由为什么会失败。 The solution contains a controller that derives from the
[ApiController]
to adhere to Web Api standards. 该解决方案包含一个从
[ApiController]
派生的控制器,以遵守Web Api标准。
Area > > > Devices > > > Sampler (Controller)
/api/Devices/Sampler/GetSamples/1
{area:exists}/{controller}/{action}/{id?}
My notion that the third line template would correlate representing the first and second line when MapAreaRoute
is utilized. 我的想法是,当使用
MapAreaRoute
时,第三行模板将相互关联,代表第一行和第二行。
application.UseMvc(routes =>
{
routes.MapAreaRoute(
name: "Device Route",
areaName: "Devices",
template: "api/{area:exists}/{controller}/{action}/{id?}
);
}
Even if I remove api
from template, specify the area, controller, and action I still receive a 404. 即使我从模板中删除
api
,指定区域,控制器和操作,我仍然会收到404。
[ApiController]
[Area("Devices")]
[Route("api/[controller]")]
public class SamplerController : ControllerBase
{
[HttpGet("{id}")]
public JsonResult GetSamples(Guid id) => new JsonResult("...");
}
Could someone elaborate on why the area does not work? 有人可以详细说明为什么该区域不起作用吗? If I create a directory called Controllers, then use
MapRoute
with the same above code the route works as intended. 如果我创建一个名为Controllers的目录,则将
MapRoute
与上面的相同代码一起使用,即可按预期工作。
You need to define the area this controller belongs in by using an attribute per the docs . 您需要根据docs使用一个属性来定义此控制器所属的区域。
[Area("Products")]
public class ManageController : Controller
Update: Also you defined api/{area:exists}/{controller}/{action}/{id?}
as your route and per your comment you are trying api/devices/sampler/8
. 更新:另外,您还定义了
api/{area:exists}/{controller}/{action}/{id?}
作为路由,并根据您的评论尝试使用api/devices/sampler/8
。 So using your route as a guide your URL should be api/devices/sampler/GetSamples/8
. 因此,以您的路线为指导,您的网址应为
api/devices/sampler/GetSamples/8
。 You need to include the action name in the url since that is how its laid out in your route: api/{area:exists}/{controller}/{action}/{id?}
您需要在网址中包含操作名称,因为这是您在路由中的布局方式:
api/{area:exists}/{controller}/{action}/{id?}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.