I'm using MVC 5, I have this url:
/Joes-studio-essex
This is 2 seperate params {business}-{county}
Q1) How can i write a route to split this out??
Q2) Can anyone think of a way to deal with this url '/joes-studio-west-yorkshire', where the county is 'west-yorkshire' ?
Thanks
There's no way for MVC to determine how to split up that string so you have to do it yourself. Create an action method with this signature:
public ActionResult Index(string businessAndCounty)
{
}
Inside the function, you can then do the splitting up of the string manually. Note this is completely untested and is virtually psuedo code so needs fixing and lots of error handling adding:
string[] parts = businessAndCounty.Split('-');
int index = 0;
string business = parts[index];
while(!BusinessExists(business) && index < parts.Count)
{
index++;
business = string.Format("{0}-{1}", business, parts[index]);
}
string county = string.Join(parts.Skip(index), "-");
return BusinessAndCounty(business, county);
So now your controller looks something like this:
public class HomeController
{
public ActionResult Index(string businessAndCounty)
{
string[] parts = businessAndCounty.Split('-');
int index = 0;
string business = parts[index];
while(!BusinessExists(business) && index < parts.Count)
{
index++;
business = string.Format("{0}-{1}", business, parts[index]);
}
string county = string.Join(parts.Skip(index), "-");
return BusinessAndCounty(business, county);
}
private bool BusinessExists(string business)
{
//Your logic to determine if the business exists
}
private ActionResult BusinessAndCount(string business, string county)
{
//your logic here
}
}