考虑控制器CustomerController.cs
上的两种方法:
//URL to be http://mysite/Customer/
public ActionResult Index()
{
return View("ListCustomers");
}
//URL to be http://mysite/Customer/8
public ActionResult View(int id)
{
return View("ViewCustomer");
}
答案 0 :(得分:1)
在global.asax.cs中,添加以下内容(假设您使用默认的mvc visual studio模板)
Route.MapRoute("Customer",
"Customer/{id}",
new { Controller = "CustomerController", action="View", id="" });
确保将此路线放在模板
中的默认路线之前然后您需要修改您的控制器。对于视图,
public ActionResult View(int? id)
{
if (id == null)
{
return RedirectToAction("Index"); // So that it will list all the customer
}
//...The rest follows
}
对于第二个问题,ActionLink很简单。
Html.ActionLink("Link Text", "View", "Customer", new {id=1}, null);