0

I have my default MVC routes setup as:

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

What I want to be able to do is have the following routes in my Search controller hit.

.../Search/Uk
.../Search/Uk/County/Buckinghamshire
.../Search/Uk/City/London
.../Search/Uk/Town/Ashford
.../Search/Uk/Postcode/AB-Aberdeen

I only have one view called "Index". As I understood routing I presumed I should of been able to do something like this:

public ActionResult Index(string country)

public ActionResult Index(string country, string searchType, string location)

But no cigar, anyone understand what I'm doing wrong, do I need to add in some sort of routes configuration? Infact implementing this I cannot even load the search page

1 Answer 1

1

You can use attribute based routing where you can pass parameters in the route itself.

like,

//I hope you have already enabled attribute routing and search controller with RoutePrefix as "search"

[Route("{country}")]
public ActionResult Index(string country)
{
  //Your business logic
}

[Route("{country}/{searchType}/{location}")]
public ActionResult Index(string country, string searchType, string location)
{
  //Your business logic
}

Enabling Attribute based routing : MSND

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

4 Comments

I don't seem to have a WebApiConfig class, I have BundleConfig, FilterConfig, IdentityConfig, RouteConfig, Startup.Auth
Check your RouteConfig class, where is your routes.MapRoute() written?
needed this also for my setup [Route("{Search}/{country}/{searchType}/{location}")]
Please check my comment, It is better to use RoutePrefix if you want controller name should be in route.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.