开发者

Add route for asp.net mvc 3

开发者 https://www.devze.com 2023-04-04 04:23 出处:网络
I need to be able to hande routes like this: appdomain/city/City-state, so in case somebody used appdomain/Washington/Washington-DC he retri开发者_运维技巧eves proper info from proper controller actio

I need to be able to hande routes like this: appdomain/city/City-state, so in case somebody used appdomain/Washington/Washington-DC he retri开发者_运维技巧eves proper info from proper controller action. For now can`t get what controller and action it should be to get this url and handle it properly.

To clear it a bit, there`s like no controller and action, but 2 parameters instead of them.


Why not adding a little help from a fixed path, like Show-City

routes.MapRoute(
    "CityAndState",
    "Show-City/{city}/{state}",
    new { controller = "Cities", action = "Index", id = UrlParameter.Optional }
);

this will never interfere with your existing routes, and then you can use:

http://domain.com/Show-City/New York/NY

at your Index Action inside the Cities Controller you would have something like:~

public class CitiesController : Controller
{
    public ActionResult Index(string city, string state)
    {
        // use city and state variables here

        return View();
    }
}


Try this:

routes.MapRoute("Foo", "{state}/{city}",
    new { controller = "ControllerName", action = "ActionName" });

and in your class you'd have:

public class ControllerNameController : Controller {
    public ActionResult ActionName(string state, string city) {
         ...
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号