URL 路径重复 ASP.NET
URL path is being repeated ASP.NET
我在 https://localhost:44311/
并且我有那两个按钮
当我按下“客户”按钮时,我想转到 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)
您在 RouteConfig.cs
中的 Default
路线如下所示:
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://localhost:44311/
并且我有那两个按钮
当我按下“客户”按钮时,我想转到 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)
您在 RouteConfig.cs
中的 Default
路线如下所示:
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} );