2013-08-31 3 views
0

this website 좋은 URL 있습니다. 그러나 그런 경로를 만드는 방법을 모르겠습니다. 내 형식은 다음과 같습니다.ASP.NET MVC 4 라우팅 친화적 인 URL

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 

의견이 있으십니까?

답변

0

Jammer와 동의하면 http://attributeroute.net 가야합니다. 그러나 나는 당신이 뒤에있을 것이라고 생각합니다 ...

그러나 두 경로는 함께 작업 할 수 없습니다. "최고의 스시 오브 - 일본"은 먼저 데이터베이스에 가지 않고 레스토랑의 카테고리 또는 이름입니다. 라우팅은 컨트롤러 및 데이터베이스 이전에 발생하므로 올바른 컨트롤러 작업을 수행하는 데 필요한 정보가 없습니다.

MVC 라우팅은 패턴 일치에서 작동하므로 다른 경로를 사용하려면 두 경로가 필요합니다. 당신이 할 수있는 한 가지 방법은 ...

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

마지막으로. 레스토랑 이름으로 나라가 필요한 이유가 있습니까? 같은 이름의 여러 레스토랑이있을 가능성이 있다고 가정하면 분명히 같은 나라에서 가능할 것입니다 ...

+0

+1 ... 나는 국가별로 레스토랑을 분리하고 싶습니다. 예 : domain/japanese/restaurant -> 일본어 레스토랑 목록을 보여줍니다. 도메인/korean/restaurant -> 한국어 식당 목록을 보여줍니다. domain/japanese/the-restaurant-name ---> "the-restaurant-name"이라는 레스토랑의 상세 정보 표시 – Jeph

1

당신은 그것은 당신이 클래스에이 같은 일을 할 수 있습니다 http://attributerouting.net/

사용에 보일 것입니다 : 다음

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

그리고 이것이 당신의 방법에 :

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

그래서 URL이 그 다음 :

http://www.mydomain/my/create