gpt4 book ai didi

c# - 特殊 MVC 路由不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:23 29 4
gpt4 key购买 nike

我的 MVC 项目中的路由无法正常工作...

我希望我所有的 View 都在 Views > Shared 文件夹中,如下所示:

Error.cshtml (default)
Index.cshtml (default)
Overview.cshtml (custom that I made)
Recordings.cshtml (custom that I made)

然后我创建了一个共享 Controller 来处理所有这样的 View :

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

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

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

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

我的 RouteConfig.cs 看起来像这样:

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

// Map to specific pages under Shared controller:
routes.MapRoute("SharedPages", "{action}/{id}",
new { controller = "Shared", action = @"Overview|Recordings", id = UrlParameter.Optional });

// Use the default rout for all other pages:
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Shared", action = "Index", id = UrlParameter.Optional }
);

// Show the Error page for anything else (404):
routes.MapRoute("Error", "{*url}",
new { controller = "Shared", action = "Error" }
);
}

我希望路由像这样工作:

://(url)/ (root - no action specified) --> Shared/Index.cshtml
://(url)/Index --> Shared/Index.cshtml
://(url)/Overview --> Shared/Overview.cshtml
://(url)/Recordings --> Shared/Recordings.cshtml
://(url)/whatever (or if an error occurs) --> Shared/Error.cshtml

但它没有按预期工作。如果我转到 ://(url)/(root),我会得到一个 HTTP 404 - 找不到资源。 例如,如果我转到 ://(url)/Overview,它工作正常。

我怎样才能让它像我想要的那样工作?

最佳答案

映射路线的顺序很重要,首先匹配的路线获胜。这意味着即使那里没有资源,它也会使用它匹配的路由。

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

// Use specific rout for all other pages:
routes.MapRoute("WhateverA", "WhateverA/{action}/{id}",
new { controller = "WhateverA", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute("WhateverB", "WhateverB/{action}/{id}",
new { controller = "WhateverB", action = "Index", id = UrlParameter.Optional }
);

// Map to specific pages under Shared controller:
routes.MapRoute("RootPages", "{action}/{id}",
new { controller = "Shared", action = "Index", id = UrlParameter.Optional });

// Show the Error page for anything else (404):
routes.MapRoute("Error", "{*url}",
new { controller = "Shared", action = "Error" }
);
}

DefaultSharedPages 路由的问题在于它们相互冲突。如果存在,您可能需要为其他 Controller 提供特定路由。否则,另一种选择是对其他 Controller 使用属性路由,对根路由和错误使用基于约定的路由

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

//Attribute routing
routes.MapMvcAttributeRoutes();

// Map to specific pages under Shared controller:
routes.MapRoute("RootPages", "{action}/{id}",
new { controller = "Shared", action = "Index", id = UrlParameter.Optional });

// Show the Error page for anything else (404):
routes.MapRoute("Error", "{*url}",
new { controller = "Shared", action = "Error" }
);
}

相应地装饰 Controller

[RoutePrefix("WhatEver")]
public class WhatEverController : Controller {
//GET whatever
[HttpGet]
[Route("")]
public ActionResult Index() { ... }
}

关于c# - 特殊 MVC 路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42262909/

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