简体   繁体   English

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

[英]Url not hitting MVC attribute based routing controller

I am using Attribute based routing in a MVC application. 我在MVC应用程序中使用基于属性的路由。 My Code is - 我的代码是-

[RouteArea("MasterData")]
[RoutePrefix("BrandFacilityShipmentMaintenance")]
public class BrandFacilityShipmentMaintenanceController : Controller
{
    [Route("Index")]
    public ActionResult Index()
    {

    }
}

I am trying to hit the url with variabale parameters like 我正在尝试使用类似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

But it says resource not found. 但是它说找不到资源。 These all urls hit same Index action in Conventional based routing. 在基于常规的路由中,所有这些URL均击中相同的Index操作。 What should I change to make it work in attribute based routing. 我应该进行哪些更改才能使其在基于属性的路由中工作。

AreaRegistration.cs - 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 }
    );
}

The url parameters are mapped to the method's parameters, sou you need to specify them in your method's signature. url参数映射到方法的参数,因此您需要在方法的签名中指定它们。

public string Index(int id, int? pid)  { ... }

From here 这里

EDIT: You can also access your query string parameters this way: 编辑:您还可以通过这种方式访问​​您的查询字符串参数:

public ActionResult Index(int id)
{ 
    string param1 = this.Request.QueryString["pid"];
    // parse int or whatever
}

EDIT2: This is also a good reading EDIT2: 也是一本好书

You are probably combining convention based routing with attribute routing, and you should register your areas after you map the attribute routes. 您可能将基于约定的路由与属性路由相结合,并且在映射属性路由后应注册区域。

Add the area registration in Application_Start() after RouteConfig.RegisterRoutes(RouteTable.Routes) RouteConfig.RegisterRoutes(RouteTable.Routes)之后在Application_Start()添加区域注册

AreaRegistration.RegisterAllAreas();

Try to use the named parameter "AreaPrefix" in RouteArea 尝试在RouteArea中使用命名参数“ AreaPrefix”

[RouteArea("MasterData", AreaPrefix = "MasterData")]

It should work. 它应该工作。

Also you can remove the RouteArea attribute and use only RoutePrefix in following way 您也可以删除RouteArea属性,并按以下方式仅使用RoutePrefix

[RoutePrefix("MasterData/BrandFacilityShipmentMaintenance")]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC属性路由未命中正确的控制器 - ASP.NET MVC attribute routing not hitting correct controller MVC5基于属性的路由 - MVC5 Attribute based routing MVC控制器路由,在url中更改控制器名称 - MVC Controller routing, change the name of controller in url MVC API 未命中 controller - MVC API not hitting controller 使用属性路由从URL中删除控制器名称 - Remove controller name from URL with Attribute routing MVC自定义视图路由基于控制器结构 - MVC custom view routing based on controller structure 如何在启动文件上配置基于属性的路由,而不是在 asp.net 核心 mvc 中使用 controller 动作上的属性 - How can i configure attribute based routing on startup file instead of using attribute on controller action in asp.net core mvc 是否可以在同一个控制器中使用属性路由和基于约定的路由? - Is it possible to use attribute routing and convention based routing in the same controller? 同时使用属性路由和基于约定的路由以及Web API和MVC - Using attribute routing and convention based routing with Web API and MVC simultaneously 如何在MVC属性路由中设置控制器名称? - How to set the controller's name in MVC attribute routing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM