[英]ASP.Net MVC : How to do extract and automatic data mapping
假設我有像www.mysite.com/Certificate/data/C001-P2003這樣的鏈接
我的控制器名稱是證書和操作名稱數據
public class CertificateController : Controller
{
public ActionResult Data(string certid,string studid)
{
return View();
}
}
我想知道我需要開發什么來提取 id 並分成由連字符分隔的兩部分,並將兩個數據映射到數據操作。
C001 將映射到 certid,P2003 將映射到studid。 所以我需要概念怎么做?
我需要開發任何動作過濾器嗎?
我需要編寫任何自定義值提供程序嗎?
我需要編寫任何自定義模型綁定器嗎?
我不想在 action 中破壞數據,而是想在 action 之外做它。 所以建議我一些最好的主意。 謝謝
在您的RouteConfig
中創建一個約定來映射您想要的路線。
routes.MapRoute(
name: "CertificateData",
url: "Certificate/data/{certid}-{studid}",
defaults: new { controller = "Certificate", action = "Data"}
);
或者啟用屬性路由並在操作上使用模板來匹配所需的 url
// GET Certificate/data/C001-P2003
[Route("Certificate/data/{certid}-{studid}")]
public ActionResult Data(string certid,string studid) {...}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.