【问题标题】:My routing isn't working我的路由不起作用
【发布时间】:2013-05-15 17:34:54
【问题描述】:

我有一个这样的视图文件夹结构:

Views
    Rooms
        Resorts
            Index.cshtml
            Suites.cshtml
            .....

我希望控制器文件夹结构匹配

Controllers
    Rooms
        ResortsController.cs

我添加了一个新的 mapRoute

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

但是没有找到视图的控制器。我试过了 本地主机/房间和 本地主机/房间/度假村/索引 -

两者的结果相同。

我错过了什么?

编辑:根据业务需求,我需要 URLS 看起来像客房/度假村/套房等,并且我需要在不使用区域的情况下执行此操作。我将有多个使用相同操作名称的 URL,例如 Rooms/Resorts/Suites、Rooms/Suites/Suites、Rooms/AwesomeSuites/Suites。因此,使用子文件夹是有必要的 - 组织并拥有多个名为 Suites 的视图。

在不使用区域的情况下我需要做的事情可能吗?

【问题讨论】:

  • 你遇到了什么异常?
  • 在 Views 或 Controllers 下使用子目录是没有意义的。

标签: c# asp.net-mvc-4 asp.net-mvc-routing


【解决方案1】:

好吧,据我所知,路由并不关心您创建的那个额外文件夹(房间),因此它很可能会尝试从 Views/Resorts/ 而不是 Views/Rooms/Resorts 解析视图。如果你需要这种分离,比如在结构的其他地方有一个 ResortsController,你应该使用 Areas。例如,您可以创建一个名为 Rooms 的区域!

【讨论】:

  • 我不能使用区域,因为我们将把我们的应用程序移动到目前不支持区域的 Sitecore。
  • @TheGeekYouNeed - 你还没有回答核心问题:你有重复的控制器名称吗?
  • 不,控制器名称将与我上面问题中的示例 URL 匹配,因此 Resorts.cs、Suites.cs、AwesomeSuites.cs 等。
【解决方案2】:

你可以试试:

routes.MapRoute( “房间”, "房间/度假村/{id}", 新的 {controller = "Resorts", action = "Index", id = UrlParameter.Optional} );

【讨论】:

    【解决方案3】:

    我解决了我的问题。在我的问题中使用我的映射路由,并保持我实现的文件夹结构完整,我创建了一个自定义视图引擎,它将动态创建一个路径来查找视图。

    public class CustomRazorViewEngine : RazorViewEngine
    {
        public CustomRazorViewEngine()
        {
            ViewLocationFormats = new string[] { "~/Views/%1/{1}/{0}.cshtml"};
            MasterLocationFormats = new string[] { "~/Views/%1/{1}/{0}.cshtml",
                           "~/Views/Shared/%1/{0}.cshtml"};
            PartialViewLocationFormats = new string[] { "~/Views/Rooms/{1}/{0}.cshtml",
                            "~/Views/Shared/%1/{0}.cshtml"};
            FileExtensions = new string[] { "cshtml"};
        }
    
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            var path = GetPath(controllerContext.Controller.GetType().Namespace);
            return base.CreatePartialView(controllerContext, partialPath.Replace("%1", path));
        }
    
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            var path = GetPath(controllerContext.Controller.GetType().Namespace);
            return base.CreateView(controllerContext, viewPath.Replace("%1", path), masterPath.Replace("%1", path));
        }
    
        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            var path = GetPath(controllerContext.Controller.GetType().Namespace);
            return base.FileExists(controllerContext, virtualPath.Replace("%1", path));
        }
    
    
        private string GetPath(string nameSpace)
        {
            var split = nameSpace.Split('.');
            int startingIndex = 0;
            StringBuilder sb = new StringBuilder();
    
            foreach(string s in split)
            {
                startingIndex++;
    
                if (s == "Controllers")
                {
                    break;
                }
            }
    
            for(int x = startingIndex; x < split.Length; x++)
            {
                sb.Append(split[x]);
                if (x != split.Length - 1)
                {
                    sb.Append("/");
                }
            }
    
            return sb.ToString();
        }
    

    所以只要View文件夹和Controllers文件夹中的文件夹结构相同,CustomRazorViewEngine就会找到视图。

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 2022-01-19
      • 1970-01-01
      • 2017-10-31
      • 2019-11-03
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多