0

I am using Attribute based routing in a MVC application. 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

/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. What should I change to make it work in attribute based routing.

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 }
    );
}
2
  • post your RouteConfig code for better clarity Commented May 16, 2019 at 9:43
  • @MannanBahelim posted Commented May 16, 2019 at 9:59

2 Answers 2

1

The url parameters are mapped to the method's parameters, sou you need to specify them in your method's signature.

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

Sign up to request clarification or add additional context in comments.

Comments

0

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)

AreaRegistration.RegisterAllAreas();

Try to use the named parameter "AreaPrefix" in RouteArea

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

It should work.

Also you can remove the RouteArea attribute and use only RoutePrefix in following way

[RoutePrefix("MasterData/BrandFacilityShipmentMaintenance")]

1 Comment

I didn't see AreaPrefix in your code so suggested that. Try adding AreaPrefix in RouteArea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.