[英]Url not hitting MVC attribute based routing controller
我在MVC應用程序中使用基於屬性的路由。 我的代碼是-
[RouteArea("MasterData")]
[RoutePrefix("BrandFacilityShipmentMaintenance")]
public class BrandFacilityShipmentMaintenanceController : Controller
{
[Route("Index")]
public ActionResult Index()
{
}
}
我正在嘗試使用類似variabale參數的網址
/MasterData/BrandFacilityShipmentMaintenance/Index
/MasterData/BrandFacilityShipmentMaintenance/Index/1156?pid=1120
/MasterData/BrandFacilityShipmentMaintenance/Index/1156?pid=1120&fname=Brand+Facility+Shipment+Maintenanca
/MasterData/BrandFacilityShipmentMaintenance/Index/1156?pid=1120&fname=Brand+Facility+Shipment+Maintenanca&isReffered=false
但是它說找不到資源。 在基於常規的路由中,所有這些URL均擊中相同的Index操作。 我應該進行哪些更改才能使其在基於屬性的路由中工作。
AreaRegistration.cs-
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.MapMvcAttributeRoutes();
context.MapRoute(
"Masterdata_default",
"Masterdata/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
您可能將基於約定的路由與屬性路由相結合,並且在映射屬性路由后應注冊區域。
在RouteConfig.RegisterRoutes(RouteTable.Routes)
之后在Application_Start()
添加區域注冊
AreaRegistration.RegisterAllAreas();
嘗試在RouteArea中使用命名參數“ AreaPrefix”
[RouteArea("MasterData", AreaPrefix = "MasterData")]
它應該工作。
您也可以刪除RouteArea屬性,並按以下方式僅使用RoutePrefix
[RoutePrefix("MasterData/BrandFacilityShipmentMaintenance")]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.