Question

I found this website have nice URLs. But I don't know how to create routes like that. My format is like:

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

Any suggestions?

Était-ce utile?

La solution 2

Agree with Jammer that http://attributeroute.net is the way to go. However I think what you would be after is the following...

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

However, the two routes you have ARE IMPOSSIBLE to work together. How do you work out that "the-best-sushi-of -japanese" is a Category or a Name of a Restaurant, without first going to the database. Since Routing happens BEFORE the controller, and hence the database, thus you don't have the information required to do the correct Controller Action.

MVC routing works on pattern matching, so you need the two routes to have different PATTERNS. One way you could do this is...

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

Finally though. Is there any reason why you NEED the country with the restaurant name? If you assume that there is the possiblity of multiple restaurants with the same name, then surely it is more likely within the same country...

Autres conseils

You should look into using http://attributerouting.net/

It lets you do things like this on your class:

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

And then this on your method:

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

So your url is then:

http://www.mydomain/my/create
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top