C# Url未命中基于MVC属性的路由控制器
我正在MVC应用程序中使用基于属性的路由。我的密码是-C# Url未命中基于MVC属性的路由控制器,c#,.net,asp.net-mvc,asp.net-mvc-routing,C#,.net,Asp.net Mvc,Asp.net Mvc Routing,我正在MVC应用程序中使用基于属性的路由。我的密码是- [RouteArea("MasterData")] [RoutePrefix("BrandFacilityShipmentMaintenance")] public class BrandFacilityShipmentMaintenanceController : Controller { [Route("Index")] public ActionResult Index() { } } 我正在尝试使
[RouteArea("MasterData")]
[RoutePrefix("BrandFacilityShipmentMaintenance")]
public class BrandFacilityShipmentMaintenanceController : Controller
{
[Route("Index")]
public ActionResult Index()
{
}
}
我正在尝试使用如下参数点击url
/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都会执行相同的索引操作。我应该改变什么使它在基于属性的路由中工作
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.RegisterRoutesRouteTable.Routes之后添加应用程序_Start中的区域注册 尝试在RouteArea中使用命名参数AreaPrefix
[RouteArea("MasterData", AreaPrefix = "MasterData")]
它应该会起作用
您还可以删除RouteArea属性,并按照以下方式仅使用RoutePrefix
[RoutePrefix("MasterData/BrandFacilityShipmentMaintenance")]
url参数映射到方法的参数,因此需要在方法的签名中指定它们
public string Index(int id, int? pid) { ... }
从
编辑:
您还可以通过以下方式访问查询字符串参数:
public ActionResult Index(int id)
{
string param1 = this.Request.QueryString["pid"];
// parse int or whatever
}
EDIT2:也是一本不错的读物发布您的RouteConfig代码,以获得更好的效果clarity@MannanBahelimpostedI在您的代码中没有看到AreaPrefix,因此建议这样做。尝试在RouteArea中添加AreaPrefix
public ActionResult Index(int id)
{
string param1 = this.Request.QueryString["pid"];
// parse int or whatever
}