我進去了https://localhost:44311/,我有這 2 個按鈕

當我按下客戶按鈕時,我想去https://localhost:44311/Customers查看當前客戶的串列。同樣https://localhost:44311/Movies,查看電影串列。對于這兩個,我有兩個控制器,分別命名為MoviesController和CustomersController。這是我的代碼CustomersController:
namespace MovieLab.Controllers
{
public class CustomersController : Controller
{
public ActionResult AllCustomers()
{
var customers = new List<Customer>
{
new Customer(){ Name = "Customer 1"},
new Customer(){ Name = "Customer 2" }
};
var customerViewModel = new CustomerViewModel()
{
Customers = customers
};
return View(customerViewModel);
}
}
當我構建上面的代碼時,我的 URL 看起來https://localhost:44311/Customers/AllCustomers
不應該是這樣https://localhost:44311/AllCustomers嗎?(我將其命名為AllCustomers,因此 URL 看起來不像Customers/Customers)
uj5u.com熱心網友回復:
您的Default路線RouteConfig.cs如下所示:
url: "{controller}/{action}/{id}"
這將生成一個 url,如:
https://localhost:44311/Customers/AllCustomers
現在要生成所需的 url,您需要將路由設定為(在默認路由之前添加):
routes.MapRoute(
name: "MyRoute",
url: "allcustomers",
defaults: new { controller= "Customers", action = "AllCustomers", id = UrlParameter.Optional }
);
// default route
routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional} );
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433737.html
標籤:C# 网 asp.net-mvc
