繁体   English   中英

网址未达到基于MVC属性的路由控制器

[英]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 }
    );
}

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.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM