Asp.net MVC路由id编辑或新建

本文关键字:编辑 新建 id 路由 net MVC Asp | 更新日期: 2023-09-27 18:17:31

我有一个这样的问题。这是我的默认路由配置。

 routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Index", action = "Index", id = UrlParameter.Optional },
        namespaces: new string[] { "App.Controllers" }
       );

我需要这样的东西。

www.xx.com/transaction/index/-> List Transactions

www.xx.com/transaction/new/->New Transaction

www.xx.com/transaction/new/XXX -> Edit Transaction

我该怎么办?

Asp.net MVC路由id编辑或新建

public class TransactionController 
{
    public ActionResult Index()
    {
       return View("List", yourList);
    }
    [HttpGet]
    public ActionResult New(int? id)
    {
       var model = new Model();
       if(id.HasValue)
       {
           model = Get(id.Value);
       }
       return View("New", model);
    }
 }