2012-01-08 77 views
0

也许我不明白asp mvc路由的真正目的。
我创建了一个应用程序,现在我需要修复我的网址a以便更容易理解。
例如,我有控制器Home和动作Index的区域Cities
所以在这里我需要像这样的网址:localhost/London但目前的路由我得到localhost/cityPage/Home
我的问题是我可以以某种方式传递像city name这样的参数并使URL像我想要的那样?
这是我的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) 

回答

1

为了路由URL本地主机/伦敦到城市地区HomeController的索引行动,你需要这样的路线:

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" 
    } 
); 

这会让你的URL看起来像本地主机/城市/伦敦。这可以接受吗?

更新1

除非你彻底删除你的“默认”路线定义,你会真正有映射到该动作的多个呼入路由。您将有localhost/cities/London,localhost/cityPage/Home,localhost/cityPage/Home/Indexlocalhost/cityPage/Home/Index/London全部解决该操作。但是,当MVC选择生成OUTBOUND路线时,它会选择第一个 - localhost/cities/London。

更新2

如果你希望你的路线参数是的cityName,你可以这样做:

routes.MapRoute(null, 
    "cities/{cityName}", 
    new 
    { 
     area = "Cities", controller = "Home", action = "Index" 
    } 
); 

然而,你将不得不改变你的城市地区的HomeController中的索引操作有这个签名:

public ActionResult Index(string cityName) 

通过改变参数从id到cityName,你告诉MVC通过这个网址参数/路线段到操作方法。

更新3

是您的区域 “城市” 或 “CityPage” 的名字?根据之前的代码,它看起来像您所在地区的名称是Cities。

如果是CitiesPage,尝试一下本作的操作方法:

@Html.ActionLink("City London", "Index", "Home", 
    new { area = "CityPage", cityName = "London" }) 

最终答案

我只是转载此在MVC3项目,并正在按预期:

  1. 创建了一个名为“CityPage”的新区域
  2. 添加了一个带有索引ac的HomeController给CityPage区域
  3. 为CityPage/Views/Home文件夹添加了一个索引视图。

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> 
+0

我加入这个默认之前,但URL不瓒ged :(我如何控制{id}参数? – 1110 2012-01-08 15:02:07

+0

请将您想要控制的{id}参数添加到您的问题中。 – danludwig 2012-01-08 15:12:53

+0

我添加了我的家庭/索引操作方法代码,也许我犯了一些错误。 – 1110 2012-01-08 15:17:35

0

是的,你可以做到这一点的方式,但你必须做以下的事情

  1. 确保您的路线通用路由之前必须注册。
  2. 获取有关RouteConstraint

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

刚例如尝试这种方式检查您请求的网址本地主机/伦敦

routes.MapRoute(
       "Default", 
       "{id}", 
       new { area = "CityPage", controller = "Home", action = "Index", id = "" }, 
       new string[] { "MyProject.Areas.Cities.Controllers" }).DataTokens.Add("area", "Cities");