0

このウェブサイトには素敵な 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

助言がありますか?

4

2 に答える 2

1

http://attributerouting.net/の使用を検討する必要があります

クラスで次のようなことができます。

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

そして、これはあなたの方法で:

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

したがって、あなたのURLは次のとおりです。

http://www.mydomain/my/create
于 2013-08-31T09:43:37.877 に答える
0

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

ただし、2 つのルートを一緒に使用することはできません。最初にデータベースにアクセスせずに、「日本の最高の寿司」がカテゴリまたはレストランの名前であることをどのように解決しますか. ルーティングはコントローラーの前、つまりデータベースの前に発生するため、正しいコントローラー アクションを実行するために必要な情報がありません。

MVC ルーティングはパターン マッチングで機能するため、2 つのルートが異なるパターンを持つ必要があります。これを行う1つの方法は...

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

最後ですが。レストラン名に国が必要な理由はありますか? 同じ名前のレストランが複数存在する可能性があるとすれば、確かに同じ国内にある可能性が高い...

于 2013-08-31T10:09:44.083 に答える