gpt4 book ai didi

c# - 通过更改 Razor 中的 url 获取 View

转载 作者:行者123 更新时间:2023-11-30 16:17:27 26 4
gpt4 key购买 nike

我的 asp.net mvc4 应用程序中的文件 RouteConfig.cs 有问题:

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
name: "Admin",
url: "Admin",
defaults: new { controller = "Home", action = "Administration" }
);
}

home Controller 中:

public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}

public ActionResult Administration()
{
return View();
}

并添加了 View :

views

但是当我将 http://localhost:61961/Admin 作为 url 时,找不到 View 。

为什么会这样?我该如何解决?

最佳答案

路由配置是有序的。请将您的管理配置移动到 RegisterRoutes 的顶部。

现在你会被映射到

Controller: admin
View: index

因为您的默认路由捕获您的 localhost:{port}/admin

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Admin",
url: "Admin",
defaults: new { controller = "Home", action = "Administration" }
);

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

关于c# - 通过更改 Razor 中的 url 获取 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17144379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com