In my Index page I am generating url using the following
@foreach(var c in ViewBag.cities)
{
<li>@Html.ActionLink((string)c.CityName,"somepage",new{city=c.CityName, id=c.Id})</li>
}
Which generated urls for every city in the following format
localhost:55055/1/city1
localhost:55055/2/city2
...
where 1,2 are Id and city1, city2 are CityName
I have configured the route as
routes.MapRoute(
name: "CityRoute",
url: "{id}/{city}",
defaults: new { controller = "Home", action = "somepage", id = UrlParameter.Optional, city = UrlParameter.Optional }
);
The somepage
action method in Home Controller:
public string somepage(int id, string city)
{
return city + " " + id;
}
Even I tried this
public string somepage()
{
return "Hello";
}
but it results in Resource cannot be found
I tried putting breakpoint in sompage
which is never hit.
Any suggesstions
Update
As pointed out by @KD in comments below I changed the routes order and placed above mentioned rule above all rules. I also changed the method
public ActionResult somepage(int id=0, string city="")
{
return Content(city + " " + id);
}
Now the behavior changed and the default rule
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
is not used for normal index page. Instead this use is used since the breakpoint in method somepage
is hit and If I put http://localhost:55055/2/cityname
in the browser, the page does display id and cityname. But now default route is not used for application home http://localhost:55055/
The pattern of your new route and pattern of default route is almost same hence it will always create conflict to match those routes .. change your route to following
routes.MapRoute(
name: "CityRoute",
url: "CitySearch/{id}/{city}",
defaults: new { controller = "Home", action = "somepage", id = UrlParameter.Optional, city = UrlParameter.Optional }
);
ie add some prefix to your pattern so it can get easily identified.. like "CitySearch" and then mention your route name while providing Action link for it..
Or if you don't want to add prefix to it then do the following thing and it will work like a charm..
For your CityRoute add a Route Constraint for Id which will check if the ID field is Integer. for normal URLS it will return false and hence your default route will get evaluated ... try this...
routes.MapRoute(
name: "CityRoute",
url: "{id}/{city}",
defaults: new { controller = "Home", action = "somepage", id = UrlParameter.Optional, city = UrlParameter.Optional },
constraints: new {id = @"\d+"}
);
This is the Regular Expression Constraint.. Put this route at the top and check.
As you have not defined Action it won't be called thus error is coming
Try this,
public ActionResult somepage(int id, string city)
{
return Content(city + " " + id);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.