[英]how to map this route from mvc controller to a web api controller?
我們有一個 mvc 項目,我們想轉移到 web api。 mvc 項目沒有任何 UI,因此使用 web api 控制器以外的 mvc 控制器是錯誤的。
但是,我們的客戶通過 url 訪問 mvc 控制器,例如:
https://mysite.azurewebsites.net/indexes/unit/docs/search (both httppost)
https://mysite.azurewebsites.net/indexes/unit/docs/post (both httppost)
https://mysite.azurewebsites.net/indexes/unit/docs/get
部件單元/文檔實際上是動態的(我們發送到數據庫進行一些查詢的東西)。
如果我們將這個控制器移動到 web api,我們如何處理路由,因為我們需要向后兼容?
在您的 WebApi 控制器中,執行以下操作:
[route("{controller}")]
public IndexesController : ControllerBase
{
[HttpPost("{unit}/{docs}/{search}")]//or whatever those variables should be
public async Task<IactionResult> Search(string unit, string docs, string search)
{
//Do your stuff
var result = _logic.DoStuff();
return Ok(result);
}
}
不過沒有理由離開 MVC 控制器。 總是返回一個 JsonResult() 並且從調用者的角度來看它的行為與 WebApi 完全一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.