Sana GET
ve kodunuzda orada POST
var gibi ben izleyin, ancak her durumda diğer seçenekler var emin değilim:
Seçenek 1
İlk olarak, içinde özel Yolları yapılandırabilirsiniz WebApiConfig.cs
dosyasında App_Start
klasörü.
// GET /api/{resource}/{action}
config.Routes.MapHttpRoute(
name: "Web API RPC",
routeTemplate: "{controller}/{action}",
defaults: new { },
constraints: new { action = @"[A-Za-z]+", httpMethod = new HttpMethodConstraint("GET") }
);
// GET|PUT|DELETE /api/{resource}/{id}/{code}
config.Routes.MapHttpRoute(
name: "Web API Resource",
routeTemplate: "{controller}/{id}/{code}",
defaults: new { code = RouteParameter.Optional },
constraints: new { id = @"\d+" }
);
// GET /api/{resource}
config.Routes.MapHttpRoute(
name: "Web API Get All",
routeTemplate: "{controller}",
defaults: new { action = "Get" },
constraints: new { httpMethod = new HttpMethodConstraint("GET") }
);
// PUT /api/{resource}
config.Routes.MapHttpRoute(
name: "Web API Update",
routeTemplate: "{controller}",
defaults: new { action = "Put" },
constraints: new { httpMethod = new HttpMethodConstraint("PUT") }
);
// POST /api/{resource}
config.Routes.MapHttpRoute(
name: "Web API Post",
routeTemplate: "{controller}",
defaults: new { action = "Post" },
constraints: new { httpMethod = new HttpMethodConstraint("POST") }
);
// POST /api/{resource}/{action}
config.Routes.MapHttpRoute(
name: "Web API RPC Post",
routeTemplate: "{controller}/{action}",
defaults: new { },
constraints: new { action = @"[A-Za-z]+", httpMethod = new HttpMethodConstraint("POST") }
);
Ben RESTful
uç noktalarının kombinasyonunu yanı sıra RPC
uç noktaları kullanın: Burada normalde kullandığınız yöntemdir. Bazı püristler için bu, kutsal bir savaşın gerekçesidir. Benim için, ikisinin bir kombinasyonunu kullanıyorum çünkü güçlü bir kombinasyon ve bende hiçbir akıl sebebi bulamıyorum.
Diğerleri işaret gibi
Seçenek 2
ve kendimi bugünlerde daha yapıyorum gibi kullanım nitelik yönlendirme:
[HttpGet]
[GET("SomeController/SomeUrlSegment/{someParameter}")]
public int SomeUrlSegment(string someParameter)
{
//do stuff
}
Ben bu işi yapmak için yönlendirme özelliği için bir Nuget paketi gerekli (Sadece "Yönlendirme Yönlendirmesi" için NuGet'i ara), fakat MVC 5/WebAPI 2'nin bunu doğal olarak kullandığını düşünüyorum.
Bu yardımcı olur umarım.
Merhaba! '[Route (" MyPath ")]' 'ı girmeye çalıştığımda]' 'VS2013 onu çözmez. Ayrıca hata ayıklayamıyorum. Bana [Rota ("MyPath")] 'hakkında bir hata veriyor. Sorunun ne olduğunu biliyor musun? –
@ClarkKent MVC 5 ve WebApi 2'de yereldir, aksi halde, 'AttributeRouting' –