【问题标题】:ASP.NET MVC 4 routing friendly URLASP.NET MVC 4 路由友好 URL
【发布时间】:2013-09-03 22:32:19
【问题描述】:

我发现this website 有不错的网址。但我不知道如何创建这样的路线。我的格式是这样的:

www.domain.com/{country}/{category-name}  ---> www.domain.com/japanese/restaurant
www.domain.com/{country}/{restaurant-name} ---> www.domain.com/japanese/the-best-sushi-of -japanese

有什么建议吗?

【问题讨论】:

    标签: asp.net-mvc url friendly-url asp.net-routing


    【解决方案1】:

    你应该考虑使用http://attributerouting.net/

    它可以让你在课堂上做这样的事情:

    [RoutePrefix("my")]
    public class MyController : ApiController
    

    然后在你的方法上:

    [POST("create"), JsonExceptionFilter]
    public HttpResponseMessage CreateEntry(Entry entryDc)
    

    所以你的网址是:

    http://www.mydomain/my/create
    

    【讨论】:

      【解决方案2】:

      同意 Jammer 的观点,http://attributeroute.net 是正确的选择。但是我认为你会追求的是以下......

      public class RestaurantController : ApiController
      {
          [GET("{countryId}/{categoryId}")]
          public ActionResult ListRestaurant(string countryId, string categoryId)
          {
              var restaurants = from r in db.Restaurants
                                where r.Country.CountryId == country
                                where r.Category.CategoryId == categoryId
                                select r;
              return View(restaurants);
          }
      }
      

      但是,您拥有的两条路线是不可能一起工作的。您如何确定“the-best-sushi-of -japanese”是餐厅的类别或名称,而无需先访问数据库。由于路由发生在控制器之前,因此发生在数据库之前,因此您没有执行正确的控制器操作所需的信息。

      MVC 路由适用于模式匹配,因此您需要两条路由具有不同的 PATTERNS。您可以这样做的一种方法是...

      public class RestaurantController : ApiController
      {
          [GET("{countryId}/{categoryId:int}")]
          public ActionResult ListRestaurant(string countryId, int categoryId)
          {
              var restaurants = from r in db.Restaurants
                                where r.Country.CountryId == country
                                where r.Category.CategoryId == categoryId
                                select r;
              return View(restaurants);
          }
      
          [GET("{countryId}/{restaurantName:string}")]
          public ActionResult ListRestaurant(string countryId, string restaurantName)
          {
              var restaurants = from r in db.Restaurants
                                where r.Country.CountryId == country
                                where r.Name == restaurantName
                                select r;
              var restaurant = restaurants.SingleOrDefault();
              if(restaurant == null)
                  return Redirect();///somewhere to tell them there is not restaurant with that name.
              return View(restaurants);
          }
      }
      

      最后。您有什么理由需要带有餐厅名称的国家/地区吗?如果您假设存在多个同名餐厅的可能性,那么肯定更有可能在同一个国家/地区...

      【讨论】:

      • +1... 我想按国家/地区分隔餐厅。示例:domain/japanese/restaurant --> 显示日本餐厅列表。 domain/korean/restaurant --> 显示韩国餐厅列表。 domain/japanese/the-restaurant-name ---> 显示名为“the-restaurant-name”的餐厅的详细信息
      猜你喜欢
      • 2011-04-06
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 2012-09-16
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多