也许我不明白asp mvc路由的真正目的
我创建了一个应用程序,现在我需要修复我的网址,以便更容易理解
例如,我的区域Cities
包含控制器Home
和操作Index
所以我需要这样的网址:localhost/London
但是通过当前路由我得到localhost/cityPage/Home
我的问题是,我可以以某种方式传递像city name
这样的参数,并像我想要的那样制作网址吗?
这是我在Global.asax中的当前默认路由
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "CityPage", controller = "Home", action = "Index", id = "" },
new string[] { "MyProject.Areas.Cities.Controllers" }).DataTokens.Add("area", "Cities");
新路由:
routes.MapRoute(null,
"CityPage/{cityName}",
new
{
area = "CityPage",
controller = "Home",
action = "Index"
}
);
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "CityPage", controller = "Home", action = "Index", id = "" },
new string[] { "MyProject.WebUI.Areas.CityPage.Controllers" }).DataTokens.Add("area", "CityPage");
我点击的链接示例
@Html.ActionLink("City London", "Index", "Home", new { cityName = "London" }, null)
答案 0 :(得分:1)
为了将URL localhost / London路由到Cities区域的HomeController上的Index操作,您需要这样的路由:
routes.MapRoute(null,
"{id}",
new
{
area = "Cities", controller = "Home", action = "Index"
}
);
确保在CitiesAreaRegistration.cs类的“默认”路由之前声明此路由。
但是,如果您的应用程序中有很多其他路线,添加这样的常规路线可能会对应用中的其他路线造成严重破坏。我建议添加一个URL前缀,将此路由与应用程序中的其他路由分开:
routes.MapRoute(null,
"cities/{id}",
new
{
area = "Cities", controller = "Home", action = "Index"
}
);
这会使您的网址看起来像localhost / cities / London。那可以接受吗?
更新1
除非您完全删除“默认”路由定义,否则您实际上会有多个映射到此操作的INBOUND路由。您可以localhost/cities/London
,localhost/cityPage/Home
,localhost/cityPage/Home/Index
和localhost/cityPage/Home/Index/London
全部解析该操作。但是当MVC选择生成一个OUTBOUND路由时,它将选择第一个 - localhost / cities / London。
更新2
如果您希望您的路线参数为cityName,您可以这样做:
routes.MapRoute(null,
"cities/{cityName}",
new
{
area = "Cities", controller = "Home", action = "Index"
}
);
但是,您必须更改Cities区域的HomeController上的Index操作才能拥有此签名:
public ActionResult Index(string cityName)
通过将参数从id更改为cityName,您告诉MVC将此URL参数/路由段传递给action方法。
更新3
您所在地区的名称是“城市”还是“城市页面”?从以前的代码看,您所在地区的名称看起来像是城市。
如果是CitiesPage,请尝试使用此操作方法:
@Html.ActionLink("City London", "Index", "Home",
new { area = "CityPage", cityName = "London" })
最终答案
我刚刚在一个MVC3项目中重现了这个,它正在按预期工作:
CityPageAreaRegistration.cs:
public class CityPageAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "CityPage";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(null,
"CityPage/{cityName}",
new { area = "CityPage", controller = "Home", action = "Index" }
);
//context.MapRoute(
// "CityPage_default",
// "CityPage/{controller}/{action}/{id}",
// new { action = "Index", id = UrlParameter.Optional }
//);
}
}
HomeController.cs:
public class HomeController : Controller
{
//
// GET: /CityPage/Home/
public ActionResult Index(string cityName)
{
return View();
}
}
Index.cshtml:
@{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
@Html.ActionLink("City London", "Index", "Home",
new { area = "CityPage", cityName = "London" }, null)
最后,这是由操作链接生成的链接:
<a href="/CityPage/London">City London</a>
答案 1 :(得分:0)
是的,你可以这样做,但你必须做以下事情
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs
例如,尝试这种方式检查您所需的网址localhost / London
routes.MapRoute(
"Default",
"{id}",
new { area = "CityPage", controller = "Home", action = "Index", id = "" },
new string[] { "MyProject.Areas.Cities.Controllers" }).DataTokens.Add("area", "Cities");