Attribute Routing ve'u 'dan uygulamaya çalışıyorum ancak her ikisini de bir denetleyicide kullandığımda, sürümlenen öznitelik artık çalışmıyor.Web Api 2 API Yönlendirme için Çoklu Öznitelikleri tanımayan (Sürüm oluşturma)
Öznitelik Yönlendirme ile güzel oynamak için onu özniteliğinde değiştirmek için ne gerekir? Kod Örneğin
gibi yolları değiştirmek sonra örnek projeyi indirmek (veya sadece yukarıdaki bağlantıdan birkaç dosyalarına bakmak) ve:
// When I use the RoutePrefix, VersionedRoute no longer works (Sending "Api-Version" through http header doesn't route correctly
// If I remove the RoutePrefix I can use VersionedRoute again
// What do I need to change in its code to be able to use both?
[VersionedRoute("api/Customers", 1)] // This route would be used as http://url/api/customers with a header of "api-version: 1"
[RoutePrefix("api/v1/Customers")] // This route would be used purely through url versioning of http://url/api/v1/Customers
public class CustomersV1Controller : ApiController {
/* Other stuff removed */
[VersionedRoute("api/Customer", 1)] // I'd rather not have to use this here at all and just use a single one on the class, but having both nor just one on either works right now.
[Route("")]
public IHttpActionResult Get()
{
return Json(_customers);
}
}
Düzenleme : Lütfen daha fazla bilgiye ihtiyacınız varsa, hatta fikirlerinizi iletmeniz veya bir şeyler göndermeniz gerekiyorsa lütfen bize bildirin :)
Edit2: İşte bir Ben ne yapmak istediğimin bir örneği Troy Hunt'un Blog'undan: http://www.troyhunt.com/2014/02/your-api-versioning-is-wrong-which-is.html
Edit3: Kod ve kod dizilerinin çoğunu azaltacağından, kod yazmak istediğim nokta burası.
[VersionedRoute("api/Customers", 1)] // This route would be used as http://url/api/customers with a header of "api-version: 1"
[RoutePrefix("api/v1/Customers")] // This route would be used purely through url versioning of http://url/api/v1/Customers
public class CustomersV1Controller : ApiController {
/* Other stuff removed */
[Route("")]
public IHttpActionResult Get()
{
// Removed
return Ok(customers);
}
[Route("{id:int}")]
public IHttpActionResult GetById(int id)
{
// Removed
return Ok(customer);
}
}
[VersionedRoute("api/Customers", 2)] // This route would be used as http://url/api/customers with a header of "api-version: 2"
[RoutePrefix("api/v2/Customers")] // This route would be used purely through url versioning of http://url/api/v2/Customers
public class CustomersV2Controller : ApiController {
/* Other stuff removed */
[Route("")]
public IHttpActionResult Get()
{
// Removed
return Ok(customersThatAreDifferentThanV1);
}
[Route("{id:int}")]
public IHttpActionResult GetById(int id)
{
// Removed
return Ok(customerThatIsDifferent);
}
}
Düzenleme: Son yumru, yalnızca çalışan kontrolör nitelik düzeyinde değil, işlem başına, rota başına bir kez rota sürüm bilgisini yazmak zorunda.
tam olarak yapmaya çalıştığımız şey: böylece sürüm her zaman önüne eklemeniz gerek olmazdı
bir iyileştirme yerine
Route
kendi özelliğini yaratmak olurdu? – DavidG@DavidG Her iki VersionedRoute ("Müşteri", 1) 'yi kullanmaya çalışıyorum. Bu, # sürümünün sağlanması için http üstbilgileri kullanmamı ve bir" RoutePrefix ("v1/Customer /") 'ın kullanılmasını sağlar. url üzerinden versiyonlama için. Temel olarak hem üstbilgi hem de URL sürümlerini desteklemek istiyorum. Ancak 'RoutePrefix' uygulandığında 'VersionedRoute' özniteliği çalışmayı durdurur. – John
Yapılandırmanız projeyi oluştururken neye benziyor? – entropic