Rick
Rick

Reputation: 565

How to route to a default action without it defined in the URL

How do I produce this result? www.domain.com/brand/brand-name where brand = {controller} parameter = {brand-name}

routes.MapRoute(
   name: "Brand Details",
   url: "{controller}/{brandName}",
   defaults: new { controller = "Brand", action = "Index", brandName = UrlParameter.Optional }
);

View: \Brand\Index.cshtml, which will be the default action result view.

I am getting a 404 error at this time.

Output expectation: http://www.domain.com/brand => will not be allowed http://www.domain.com/brand-name => will not be allowed http://www.domain.com/brand/brand-name => will get details of brand-name and return view.

Upvotes: 0

Views: 28

Answers (1)

user3820761
user3820761

Reputation: 26

you should route like this

routes.MapRoute(
  name: "Brand Details",
  url: "Brand/{brandName}",
  defaults: new { controller = "Brand", 
                  action = "Index", 
                  brandName = UrlParameter.Optional }
);

Upvotes: 1

Related Questions